wordpress: modifying themes

Page Hacks / Custom Templates

Sometimes you have a need to create a highly customized page in WordPress. Sure, you can always create a new standard page and put whatever you like in the content area, but what if you want to do something that utilizes template tags? Those don't work in content fields - they only work in templates.

For example, let's say you've used the WordPress Links Manager and have built up more than 100 links. That's not going to work in your sidebar - too long! You need a way to create a custom page template that can utilize any of the Template Tags in the Codex.

First, let's get comfortable with the wp_list_bookmarks() function, and use it to add links to our sidebar with it, rather than with a widget (this will give us a lot more customizability than a widget would). By calling wp_list_bookmarks() manually, you'll have full control over the way links are displayed.

Let's say you want to show only the links in categories #2 and #3, but you want them mixed (not categorized), and you want the whole block wrapped in HTML <span> tags, and you want associated images to be displayed, but you don't want descriptions used, and you want them all sorted by URL. This code (in the sidebar.php template) will do the trick:

Use a custom Page template instead

Since 100 links aren't going to fit into the typical sidebar, let's create a new Page called "Resources" with the slug "resources" (adjust the instructions below if you call your page something else). Don't add any text to the page body. Publish the page.

Log into your site via FTP and navigate to wp-content/themes/[yourtheme]. Find the page.php and duplicate/copy it. Give the new file the name page-resources.php.

Open page-resources.php in a text editor. What you see in this template depends on the exact theme you're using, but basically, you want to find the code that causes the page content to be displayed and remove it. In the case of the default "2010" theme, it's a one-liner:

Replace this with the code you want to use to display your list of links exactly the way you want them, e.g.:

What we've done is to remove the guts of the page template - the part that pulls in article content - and replaced it with code that brings in our custom bookmarks list. Season to taste!

Note the trick we used above - to create a template that applies to just one page, create a template called page-foo.php, where "foo" is the slug of the page you want to call the template. The same trick can be used if you need a certain category to utilize a custom template (but you'd use category-foo.php instead).

You can use this technique to customize the view of any Page in your system with great flexibility!