Templates and the "is" Functions

WordPress 1.3 introduces the “is” series of template functions. These tell you what type of page is being requested. For example, is_home() is true if the home page is being requested. is_category() is true if a category is being requested. Here is the full list of “is” functions and the requests for which they are true.

  • is_home() – The front page
  • is_single() – A single post
  • is_page() – A static/standalone page
  • is_category() – A category
  • is_author() – An author
  • is_date() – A year, month, or day
  • is_year() – A year
  • is_month() – A month
  • is_day() – A day
  • is_time() – An hour, minute, or second
  • is_archive() – A category, author, or date
  • is_search() – A search
  • is_404() – A 404 page
  • is_feed() – A syndication feed

Further, is_single(), is_page(), is_category(), and is_author() can accept an id, name, or nicename. Here are some examples using is_page().

  • is_page() – True if a page is being served.
  • is_page(3) – True if a page with the ID of 3 is being served.
  • is_page(‘About Me’) – True if the page title is ‘About Me’.
  • is_page(‘about-me’) – True if the page nicename is ‘about-me’.

The “is” functions are very handy for customising your template for different query types. One index.php template can provide a different look for each query type using the “is” functions. Simply use the “is” functions to form conditionals for the various request types and generate whatever markup you like for each of those conditionals.

The presence of many conditionals can make the template rather busy and difficult to read, however. An alternative is to use multiple templates. If a single post is being requested and a file named single.php is present, that will be loaded instead of the index. The same is true for categories, authors, pages, and so on. If is_category() is true and category.php is present, category.php is loaded. For each “is” function, strip the “is_” from the front and append “.php” to derive the name of the template that will be loaded if that “is” function evaluates to true. Providing multiple templates is completely optional, of course. If you would like to continue using index.php for everything, go right ahead.

The “is” functions and the query-to-template mapping provide template and theme designers a great deal of control over presentation, whether it be through one template or multiple templates. For an example of how to use the “is” functions and the template mapping to build a theme, look to the Kubrick theme.

29 thoughts on “Templates and the "is" Functions

  1. Hello!!! I’m using your Kubrick 1.3 Theme and I have an existential question. I want to create, as you have made here, “static” pages -about.php, or something.php- with static content. But I don’t understand a couple of things, and I’m looking desperately for help!
    My questions are basically 3
    1.- Where has to be placed my about.php file?
    2.- How should that page call header.php and footer.php files?
    3.- What’s the correct way for the Link in sidebar.php?

    Could you help me, please?

    Like

  2. Hi Ryan,

    I am working on a new site and am very happy with the new themes/is-functions framework. Here are two remarks, though:

    1. is_home() doesn’t really work as expected: it is also true when you are on page n>1 of a paged post display. I saw that “home” is defined as “whatever isn’t a static page, a 404 page, and archive page ….”.
    2. It would be nice to have an is-function to check precisely on which “page” of a paged post display you are (to be clear: the thing you reach by clicking on “next page” if posts_nav_link is used): I’d like have the front page look different from what the user sees when browsing back deeper into the history (like, a welcome message, a menu …).

    Thanks for all your great work.

    Like

  3. Pingback: Codex
  4. I’ve got a quest… how do you call a non-default template file… for example: links.php or superarchives.php. It was easy in 1.2, but i cant figure how to do it in 1.3 a-5…. any idea?
    thx in advance

    Like

  5. This is incredibly helpful. Thank you. Only a little whine.

    Could you please either use or fix your print.css. When I printed out the page to make sure I kept it in the file with all the new information on 1.5, the right margin cuts off the sentences so I miss two or more critical words at the end of every line.

    This info is too value to be “edited” by the printer.

    Thanks!

    Like

  6. Am I correct that these function will only work áfter the wp-blog-header is loaded? Because I tried to use these before the blog-header and nothing happened.

    Like

  7. Pingback: » Theme Creation
  8. Like whoa! Thanks so much for this – concisely explained and just what I needed. Now I can stop wading through the codex and start making useful changes to my templates. *mwah*

    Like

  9. Great reference. Helped me with the navigation highlighting on the marlyse2005 theme : boren.nu » Blog Archive » Templates and the “is” FunctionsWordPress

    Like

  10. Pingback: lineage and escape

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.