Skip to content

Templates and the "is" Functions

October 16, 2004
by Ryan Boren

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 Comments
  1. November 22, 2004 1:37 pm

    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?

  2. December 10, 2004 3:09 am

    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.

  3. December 16, 2004 11:58 am

    Great resource! Have missed it in the past few days!

    So, how, in 1.3, do you get all posts from a specific category (for eg the home page)?

  4. December 18, 2004 5:51 pm

    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

  5. January 17, 2005 11:08 am

    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!

  6. January 25, 2005 6:08 pm

    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.

  7. March 5, 2005 3:15 pm

    Could we see an example of the use of these, like in an if/else statement so we understand better how they are used? Then this would be perfect! Thanks.

  8. April 24, 2005 12:56 pm

    great info thx.

  9. June 15, 2005 8:39 pm

    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*

  10. January 20, 2006 12:36 am

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

  11. November 4, 2007 6:43 am

    Anyone know if there is a way to test what template a page uses?Something like is_template(‘name’) tag? Or of another way to check what template a page uses?

Trackbacks

  1. codex » Blog Archive » variable wordpress template
  2. Codex
  3. jogja.linux.or.id » Pembuatan Theme situs Jogja-Linux
  4. Burning River Studio » Blog Archive » Wordpress Helpfulness
  5. » Theme Creation
  6. marlyse.com » Templates and the “is” Functions
  7. marlyse.com » Templates and the “is” Functions
  8. marlyse.com » Templates and the “is” Functions
  9. hyperpup » MiniblogのCSSと不具合
  10. Taking Your Camera on the Road » WordPress Resources List
  11. waylogic.net » Blog Archive » WordPress
  12. Lorelle on WordPress » Designing a WordPress Theme From Scratch
  13. “Is” Functions in WordPress - scramble suit
  14. rawlinson - us » Blog Archive » Site has a New and Darker Look
  15. Lilithsbyte » Hello world!
  16. Blogging Web 2.0 Web Design | Social Networking | Color Tagging | Ajax | CSS | SEO | Tyic » Templates and the “is” Functions for Wordpress
  17. lineage and escape
  18. politis.ca » Testing 123

Comments are closed.