WordPress Theme Functions

wordpress-theme1. Conditional functions

To begin at the beginning, we’re talking about is_home(), is_search() etc. The whole collection of functions is here – and the majority are handled in a very similar way.

This family of conditional functions is related to the template file hierarchy – and since their use can save a lot of time and duplication of code, it’s definitely worth learning how to manipulate them efficiently.

Template Hierarchy

Depending on the initial query, WordPress ‘decides’ which type of page, an archive page, a search page, a page page, is being requested – it then searches whether there’s a specific template file in the theme directory corresponding to this request – ie search.php, archive.php, page.php.

If there is, well and good – if there isn’t, it tries the next file down in the hierarchy, and if none fit the bill, it serves up the default, index.php.

And this is where one very common use of conditional functions comes in -
Using is_search() in index.php comes to much the same thing as using a search.php file. If you’re planning only a few variations in code this is the way to go.

So, here’s an example of a control structure in index.php, testing whether the requested page is a search page, with is_search() returning a boolean value (true/false) and according to the result, adding (or not) a heading…

<?php if (is_search()) { ?>
	<h2 id="pagetitle">You searched for: <?php the_search_query(); ?></h2>
<?php } ?>

Roughly speaking, doing it this way is the better programming practice – if nothing else, there’s fewer files to go through and update if you want to make a small change in display etc. later. But best to get your head round both ways of doing it – there will be times when one or the other will be better suited to the purpose.

One slight gotcha, that’s fortunately receding into the mists of time, concerns is_home() and what happens if a static page is used as the index. Different, older, versions of WordPress will still produce variable results here – since WP2.5 the favoured test for the default home is is_front_page() – but you can’t guarantee what version of WordPress your theme might be used with, so some caution here…

And lastly, in the usual – slightly idiosyncratic – WordPress way, these are still referred to as Conditional Tags, just to create a small confusion with tags that are tags…

Next, a look at what can happen when a few values are plugged into these functions.

Share:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • LinkedIn
  • Reddit
  • StumbleUpon
  • RSS
  • Twitter

One Response to “WordPress Theme Functions”

  1. SpaceCowboy says:

    I have just installed it on WordPressMU (start-up site) and I cannot even find where it is aleged to appear in the Links section of the toolbar (admin) — I too just updated my WordPressMU to the latest patch and am having serious problems… oh yeah, I am being hosted on an IIS Server.. any help would be greatly appreciated

Leave a Reply