Introduction
The Conditional Tags can be used in your Template files to change what content is displayed and how that content is displayed on a particular page depending on what conditions that page matches. For example, you might want to display a snippet of text above the series of posts, but only on the main page of your blog. With the is_home() Conditional Tag, that task is made easy.
Note: the close relation these tags have to WordPress Template Hierarchy.
The Conditions For …
All of the Conditional Tags test to see whether a certain condition is met, and then returns either TRUE or FALSE. The conditions under which various tags output TRUE is listed below. Those tags which can accept parameters are so noted.
The Main Page
is_home() – When the main blog page is being displayed. This is the page which is showing the time based blog content of your site, so if you’ve set a static Page for the Front Page (see below), then this will only be true on the Page which you set as the “Posts page” in Administration > Settings > Reading.
The Front Page
is_front_page() – When it is the front of the site displayed, whether it is posts or a Page. Returns true when the main blog page is being displayed and the ‘Settings > Reading ->Front page displays’ is set to “Your latest posts”, or when ‘Settings > Reading ->Front page displays’ is set to “A static page” and the “Front Page” value is the current Page being displayed. Note: this tag was added at Version 2.5.
The Administration Panels
is_admin() – When the Dashboard or the administration panels are being displayed.
A Single Post Page
is_single() – When any single Post (or attachment, or custom Post Type) page is being displayed. (False for Pages)
is_single(’17’) – When Post 17 is being displayed as a single Post.
is_single(‘Irish Stew’) – When the Post with Title “Irish Stew” is being displayed as a single Post.
is_single(‘beef-stew’) – When the Post with Post Slug “beef-stew” is being displayed as a single Post.
is_single(array(17,’beef-stew’,’Irish Stew’)) – Returns true when the single post being displayed is either post ID 17, or the post_name is “beef-stew”, or the post_title is “Irish Stew”.
is_single(array(17, 19, 1, 11)) – Returns true when the single post being displayed is either post ID 17, post ID 19, post ID 1, or post ID 11.
is_single(array(‘beef-stew’, ‘pea-soup’, ‘chili’)) – Returns true when the single post being displayed is either the post_name “beef-stew”, post_name “pea-soup” or post_name “chili”.
is_single(array(‘Beef Stew’, ‘Pea Soup’, ‘Chili’)) – Returns true when the single post being displayed is either the post_title “Beef Stew”, post_title “Pea Soup” or post_title “Chili”.
Note: The array ability was added at Version 2.5. This function does not distinguish between the post ID, post title, or post name. A post named “17” would be displayed if a post ID of 17 was requested. Presumably the same holds for a post with the slug “17”.
A Sticky Post
is_sticky() – Returns true if “Stick this post to the front page” check box has been checked for the current post. In this example, no post ID argument is given, so the post ID for the Loop post is used. Note: this tag was added at Version 2.7.
is_sticky(’17’) – Returns true when Post 17 is considered a sticky post.
A Post Type
get_post_type() – Not really a conditional tag, but returns the registered post type of the current post.
if( get_post_type() == ‘book’ ) … – Tests to see if the current post is of type ‘book’.
post_type_exists() – Returns true if a given post type is a registered post type. This does not test if a post is a certain post_type. Note: This function replaces a function called is_post_type which existed briefly in 3.0 development.
A Post Type is Hierarchical
is_post_type_hierarchical($post_type) – Returns true if this $post_type has been set with hierarchical support when registered. Note: This ability was added at Version 3.0.
is_post_type_hierarchical(‘book’) – Returns true if the book post type was registered as having support for hierarchical.
A Comments Popup
is_comments_popup() – When in Comments Popup window. Any Page Containing Posts
comments_open() -When comments are allowed for the current Post being processed in the WordPress Loop.
pings_open() – When pings are allowed for the current Post being processed in the WordPress Loop.
A PAGE Page
This section refers to WordPress Pages, not any generic webpage from your blog.
is_page() – When any Page is being displayed.
is_page(’42’) – When Page 42 (ID) is being displayed.
is_page(‘About Me And Joe’) – When the Page with a post_title of “About Me And Joe” is being displayed.
is_page(‘about-me’) – When the Page with a post_name (slug) of “about-me” is being displayed.
is_page(array(42,’about-me’,’About Me And Joe’)) – Returns true when the Pages displayed is either post ID 42, or post_name “about-me”, or post_title “About Me And Joe”. Note: the array ability was added at Version 2.5.
Is a Page Template
Beginning with Version 2.5 this allows you to determine whether or not you are in a page template or if a specific page template is being used.
is_page_template() – Is a Page Template being used?
is_page_template(‘about.php’) -Is Page Template ‘about’ being used? Note that unlike with other conditionals, if you want to specify a particular Page Template, you need to use the filename, such as about.php or my_page_template.php.
A Category Page
is_category() – When any Category archive page is being displayed.
is_category(‘9’) -When the archive page for Category 9 is being displayed.
is_category(‘Stinky Cheeses’) – When the archive page for the Category with Name “Stinky Cheeses” is being displayed.
is_category(‘blue-cheese’) – When the archive page for the Category with Category Slug “blue-cheese” is being displayed.
is_category(array(9,’blue-cheese’,’Stinky Cheeses’)) – Returns true when the category of posts being displayed is either term_ID 9, or slug “blue-cheese”, or name “Stinky Cheeses”. Note: the array ability was added at Version 2.5.
in_category(‘5’) – Returns true if the current post is in the specified category id. read more
in_category( array( 1,2,3 ) ) – Returns true if the current post is in either category 1, 2, or 3.
!in_category( array( 4,5,6 ) ) – Returns true if the current post is NOT in either category 4, 5, or 6. Note the ! at the beginning.
Note: Be sure to check your spelling when testing, “is” and “in” are a big difference.
See also is_archive() and Category Templates.
A Tag Page
is_tag() – When any Tag archive page is being displayed. is_tag(‘mild’) – When the archive page for tag with the slug of ‘mild’ is being displayed. is_tag(array(‘sharp’,’mild’,’extreme’)) – Returns true when the tag archive being displayed has a slug of either “sharp”, “mild”, or “extreme”.
Note: the array ability was added at Version 2.5. has_tag() – When the current post has a tag. Must be used inside The Loop. Note: has_tag was added at Version 2.6. has_tag(‘mild’) – When the current post has the tag ‘mild’. has_tag(array(‘sharp’,’mild’,’extreme’)) – When the current post has any of the tags in the array.
See also is_archive() and Tag Templates.
A Taxonomy Page
is_tax() – When any Taxonomy archive page is being displayed.
is_tax(‘mild’) – When the archive page for taxonomy with the slug of ‘mild’ is being displayed.
is_tax(array(‘sharp’,’mild’,’extreme’)) – Returns true when the taxonomy archive being displayed has a slug of either “sharp”, “mild”, or “extreme”.
See also is_archive().
A Registered Taxonomy
is_taxonomy() – ( deprecated in 3.0 in favor of taxonomy_exists() ) – When a particular taxonomy is registered via register_taxonomy().
An Author Page
is_author() – When any Author page is being displayed.
is_author(‘4’) – When the archive page for Author number (ID) 4 is being displayed.
is_author(‘Vivian’) – When the archive page for the Author with Nickname “Vivian” is being displayed.
is_author(‘john-jones’) – When the archive page for the Author with Nicename “john-jones” is being displayed.
is_author(array(4,’john-jones’,’Vivian’)) – When the archive page for the author is either user ID 4, or user_nicename “john-jones”, or nickname “Vivian”. Note: the array ability was added at Version 2.5. -See also is_archive() and Author Templates.
A Date Page
is_date() – When any date-based archive page is being displayed (i.e. a monthly, yearly, daily or time-based archive).
is_year() – When a yearly archive is being displayed.
is_month() – When a monthly archive is being displayed.
is_day() – When a daily archive is being displayed.
is_time() – When an hourly, “minutely”, or “secondly” archive is being displayed.
See also is_archive().
Any Archive Page
is_archive() – When any type of Archive page is being displayed. Category, Tag, Author and Date based pages are all types of Archives.
A Search Result Page
is_search() – When a search result page archive is being displayed.
A 404 Not Found Page
is_404() – When a page displays after an “HTTP 404: Not Found” error occurs.
A Paged Page
is_paged() – When the page being displayed is “paged”. This refers to an archive or the main page being split up over several pages and will return true on 2nd and subsequent pages of posts. This does not refer to a Post or Page whose content has been divided into pages using the:
<!–nextpage–> QuickTag.
To check if a Post or Page has been divided into pages using the <!–nextpage–> QuickTag, see #Testing_for_paginated_Pages.
An Attachment
is_attachment() – When an attachment document to a post or Page is being displayed. An attachment is an image or other file uploaded through the post editor’s upload utility. Attachments can be displayed on their own ‘page’ or template. For more information, see Using Image and File Attachments.
A Single Page, Single Post or Attachment
is_singular() – When any of the following return true: is_single(), is_page() or is_attachment().
is_singular(‘book’) – True when viewing a post of the Custom Post Types book. Introduced with Version 3.0.
is_singular(array( ‘newspaper’, ‘book’ )) – True when viewing a post of the Custom Post Types newspaper or book. Introduced with Version 3.0.
A Syndication
is_feed() – When the site requested is a Syndication. This tag is not typically used by users; it is used internally by WordPress and is available for Plugin Developers.
A Trackback
is_trackback() – When the site requested is WordPress’ hook into its Trackback engine. This tag is not typically used by users; it is used internally by WordPress and is available for Plugin Developers.
A Preview
is_preview() – When a single post being displayed is viewed in Draft mode.
Has An Excerpt
has_excerpt() – When the current post has an excerpt
has_excerpt(’42’) – When the post 42 (ID) has an excerpt.
Leave a Reply