Conditional Tags
Conditional Tags can be used in your Template Files to alter the display of content depending on the conditions that the current page matches. They tell WordPress what code to display under specific conditions. Conditional Tags usually work with PHP if /else Conditional Statements.
1
2
3
4
5
| if ( is_user_logged_in() ): echo 'Welcome, registered user!';else: echo 'Welcome, visitor!';endif; |
Where to Use Conditional Tags
For a Conditional Tag to modify your data, the information must already have been retrieved from your database, i.e. the query must have already run. If you use a Conditional Tag before there is data, there’ll be nothing to ask the if/else statement about.
It’s important to note that WordPress loads
functions.phpbefore the query is run, so if you simply include a Conditional Tag in that file, it won’t work.
Two ways to implement Conditional Tags:
- place it in a Template File
- create a function out of it in
functions.phpthat hooks into an action/filter that triggers at a later point
This condition returns true when the main blog page is being displayed, usually in standard reverse chronological order. If your home page has been set to a Static Page instead, then this will only prove true on the page which you set as the “Posts page” in Settings > Reading.
This condition returns true when the front page of the site is displayed, regardless of whether it is set to show posts or a static page.
Returns true when:
- the main blog page is being displayed and
- the Settings > Reading -> Front page displays option 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.
This condition returns true when the Dashboard or the administration panels are being displayed.
A Single Post Page
Returns true when any single Post (or attachment, or custom Post Type) is being displayed. This condition returns false if you are on a page.
is_single( ’17’ )
is_single() can also check for certain posts by ID and other parameters. The above example proves true when Post 17 is being displayed as a single Post.
is_single( ‘Irish Stew’ )
Parameters include Post titles, as well. In this case, it proves true when the Post with title “Irish Stew” is being displayed as a single Post.
is_single( ‘beef-stew’ )
Proves true 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’, ‘chilli’ ) )
Returns true when the single post being displayed is either the post_name “beef-stew”, post_name “pea-soup” or post_name “chilli”.
is_single( array( ‘Beef Stew’, ‘Pea Soup’, ‘Chilli’ ) )
Returns true when the single post being displayed is either the post_title is “Beef Stew”, post_title is “Pea Soup” or post_title is “Chilli”.
Note: 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 Single Post, Page, or Attachment
Returns true for any is_single, is_page, and is_attachment. It does allow testing for post types.
A Sticky Post
Returns true if the “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.
is_sticky( ’17’ )
Returns true when Post 17 is considered a sticky post.
Returns true when Post 17 is considered a sticky post.
A Post Type
You can test to see if the current post is of a certain type by including get_post_type() in your conditional. It’s not really a conditional tag, but it returns the registered post type of the current post.
if ( ‘book’ == get_post_type() ) …
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
Returns true if this $post_type has been set with hierarchical support when registered.
is_post_type_hierarchical( ‘book’ )
Returns true if the book post type was registered as having support for hierarchical.
A Post Type Archive
Returns true on any post type archive.
is_post_type_archive( $post_type )
Returns true if on a post type archive page that matches $post_type (can be a single post type or an array of post types).
To turn on post type archives, use ‘has_archive’ => true, when registering the post type.
A Comments Popup
When in Comments Popup window.
Any Page Containing Posts
When comments are allowed for the current Post being processed in the WordPress Loop.
When pings are allowed for the current Post being processed in the WordPress Loop.
A “PAGE” 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 is “about-me”, or post_titleis “About Me And Joe”.
is_page( array( 42, 54, 6 ) )
Returns true when the Pages displayed is either post ID = 42, or post ID = 54, or post ID = 6.
Is a Page Template
Allows you to determine whether or not you are in a page template or if a specific page template is being used.
Is a Page Template being used?
is_page_template( ‘about.php’ )
Is Page Template ‘about’ being used? Note that unlike 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.
Note: if the file is in a subdirectory you must include this as well. Meaning that this should be the file path in relation to your theme as well as the filename, for example ‘page-templates/about.php’.
A Category Page
When a 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”.
in_category( ‘5’ )
Returns true if the current post is in the specified category id.
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. There’s a big difference between “is” or “in”.
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”.
When the current post has a tag. Must be used inside The Loop.
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
When any Taxonomy archive page is being displayed.
is_tax( ‘flavor’ )
When a Taxonomy archive page for the flavor taxonomy is being displayed.
is_tax( ‘flavor’, ‘mild’)
When the archive page for the flavor taxonomy with the slug of ‘mild’ is being displayed.
is_tax( ‘flavor’, array( ‘sharp’, ‘mild’, ‘extreme’ ) )
Returns true when the flavor taxonomy archive being displayed has a slug of either “sharp”, “mild”, or “extreme”.
Check if the current post has any of given terms. The first parameter should be an empty string. It expects a taxonomy slug/name as a second parameter.
has_term( ‘green’, ‘color’ )
When the current post has the term ‘green’ from taxonomy ‘color’.
has_term( array( ‘green’, ‘orange’, ‘blue’ ), ‘color’ )
When the current post has any of the terms in the array.
See also is_archive().
A Registered Taxonomy
When a particular taxonomy is registered via register_taxonomy(). Formerly is_taxonomy(), which was deprecated in Version 3.0
An Author Page
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”.
See also is_archive() and Author Templates.
A Multi-author Site
When more than one author has published posts for a site. Available with Version 3.2.
A Date Page
When any date-based archive page is being displayed (i.e. a monthly, yearly, daily or time-based archive).
When a yearly archive is being displayed.
When a monthly archive is being displayed.
When a daily archive is being displayed.
When an hourly, “minutely”, or “secondly” archive is being displayed.
If today is a new day according to post date. Should be used inside the loop.
Any Archive Page
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
When a search result page archive is being displayed.
A 404 Not Found Page
When a page displays after an “HTTP 404: Not Found” error occurs.
An 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.
A Single Page, Single Post or Attachment
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.
is_singular( array( ‘newspaper’, ‘book’ ) )
True when viewing a post of the Custom Post Types newspaper or book.
A Syndication
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
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
When a single post being displayed is viewed in Draft mode.
Has An Excerpt
When the current post has an excerpt
has_excerpt( 42 )
When the post 42 (ID) has an excerpt.
1234567<?php// Get $post if you're inside a function global $post;if(empty($post->post_excerpt ) ) {// This post has no excerpt}else{// This post has excerpt}?>
Other use
When you need to hide the auto displayed excerpt and only display your post’s excerpts.
When you need to hide the auto displayed excerpt and only display your post’s excerpts.
1234567<?phpif( ! has_excerpt() ) {echo'';}else{the_excerpt();}?>
Replace auto excerpt for a text or code.
123<?phpif( ! has_excerpt() ) {// you text or code} ?>
Inside The Loop
Check to see if you are “inside the loop”. Useful for plugin authors, this conditional returns as true when you are inside the loop.
Is Sidebar Active
Check to see if a given sidebar is active (in use). Returns true if the sidebar (identified by name, id, or number) is in use, otherwise the function returns false.
Part of a Network (Multisite)
Check to see whether the current site is in a WordPress MultiSite install.
Main Site (Multisite)
Determines if a site is the main site in a network.
Admin of a Network (Multisite)
Determines if a user is a network (super) admin.
Checks if a plugin is activated.
A Child Theme
Checks whether a child theme is in use.
Theme supports a feature
Checks if various theme features exist.
Working Examples
Here are working samples to demonstrate how to use these conditional tags.
Single Post
This example shows how to use
is_single() to display something specific only when viewing a single post page:
12345if( is_single() ) {echo'This is just one of many fabulous entries in the '. single_cat_title() .' category!';}
Another example of how to use Conditional Tags in the Loop. Choose to display content or excerpt in index.php when this is a display single post or the home page.
12345678910if( is_home() || is_single() ) {the_content();}else{the_excerpt();}
When you need display a code or element, in a place that is NOT the home page.
12345<?phpif( ! is_home() ) {//Insert your markup ...}?>
Check for Multiple Conditionals
You can use PHP operators to evaluate multiple conditionals in a single if statement.
This is handy if you need to check whether combinations of conditionals evaluate to true or false.
12345678// Check to see if 2 conditionals are metif( is_single() || is_page() ) ) {// If it's a single post or a single page, do something special}if( is_archive() && ! is_category('nachos') ) {// If it's an archive page for any category EXCEPT nachos, do something special}
Variable Sidebar Content
This example will display different content in your sidebar based on what page the reader is currently viewing.
Helpful 404 Page
The Creating an Error 404 Page article [NEED Link to this?] has an example of using the PHP conditional function
isset() in the Writing Friendly Messages section.In the theme’s footer.php file
At times queries performed in other templates such as sidebar.php may corrupt certain conditional tags. For instance, in header.php a conditional tag works properly but doesn’t work in a theme’s footer.php. The trick is to put wp_reset_query before the conditional test in the footer. For example:
12345<?php wp_reset_query();if( is_page('2') ) {echo'This is page 2!';}?>
Conditional Tags Index
- comments_open
- has_tag
- has_term
- in_category
- is_404
- is_admin
- is_archive
- is_attachment
- is_author
- is_category
- is_child_theme
- is_comments_popup
- is_date
- is_day
- is_feed
- is_front_page
- is_home
- is_month
- is_multi_author
- is_multisite
- is_main_site
- is_page
- is_page_template
- is_paged
- is_preview
- is_rtl
- is_search
- is_single
- is_singular
- is_sticky
- is_super_admin
- is_tag
- is_tax
- is_time
- is_trackback
- is_year
- pings_open
Function Reference
- Function: comments_open()
- Function: is_404()
- Function: is_admin()
- Function: is_admin_bar_showing()
- Function: is_archive()
- Function: is_attachment()
- Function: is_author()
- Function: is_category()
- Function: is_comments_popup()
- Function: is_date()
- Function: is_day()
- Function: is_feed()
- Function: is_front_page()
- Function: is_home()
- Function: is_local_attachment()
- Function: is_main_query
- Function: is_multi_author
- Function: is_month()
- Function: is_new_day()
- Function: is_page()
- Function: is_page_template()
- Function: is_paged()
- Function: is_plugin_active()
- Function: is_plugin_active_for_network()
- Function: is_plugin_inactive()
- Function: is_plugin_page()
- Function: is_post_type_archive()
- Function: is_preview()
- Function: is_search()
- Function: is_single()
- Function: is_singular()
- Function: is_sticky()
- Function: is_tag()
- Function: is_tax()
- Function: is_taxonomy_hierarchical()
- Function: is_time()
- Function: is_trackback()
- Function: is_year()
- Function: in_category()
- Function: in_the_loop()
- Function: is_active_sidebar()
- Function: is_active_widget()
- Function: is_blog_installed()
- Function: is_rtl()
- Function: is_dynamic_sidebar()
- Function: is_user_logged_in()
- Function: has_excerpt()
- Function: has_post_thumbnail()
- Function: has_tag()
- Function: pings_open()
- Function: email exists()
- Function: post_type_exists()
- Function: taxonomy_exists()
- Function: term_exists()
- Function: username exists()
- Function: wp_attachment_is_image()
- Function: wp_script_is()
Comments
Post a Comment