- WordPress if is blog home
- The Conditions For .
- The Main Page
- The Front Page
- The Blog Page
- The Administration Panels
- The Admin Bar
- A Single Post Page
- A Sticky Post
- A Post Type is Hierarchical
- A Post Type Archive
- A Comments Popup
- Any Page Containing Posts
- A PAGE Page
- Is a Page Template
- A Category Page
- A Tag Page
- A Taxonomy Page (and related)
- is_tax
- has_term
- term_exists
- is_taxonomy_hierarchical
- taxonomy_exists
- An Author Page
- A Multi-author Site
- A Date Page
- Any Archive Page
- A Search Result Page
- A 404 Not Found Page
- A Paged Page
- An Attachment
- Attachment Is Image
- A Local Attachment
- A Single Page, a Single Post, an Attachment or Any Other Custom Post Type
- Post Type Exists
- Is Main Query
- A New Day
- A Syndication
- A Trackback
- A Preview
- Has An Excerpt
- Has A Nav Menu Assigned
- Inside The Loop
- Is Dynamic SideBar
- Is Sidebar Active
- Is Widget Active
- Is Blog Installed
- Right To Left Reading
- Part of a Network (Multisite)
- Main Site (Multisite)
- Admin of a Network (Multisite)
- Is User Logged in
- Email Exists
- Username Exists
- An Active Plugin
- A Child Theme
- Theme supports a feature
- Has Post Thumbnail
- Script Is In use
- Is Previewed in the Customizer
- Working Examples
- Single Post
- Check for Multiple Conditionals
- Date-Based Differences
- Variable Sidebar Content
- Helpful 404 Page
- Dynamic Menu Highlighting
- Developer Resources
- Contents
- Description
- See also
- Return
- More Information
- History
- Usage
- Notes
- Developer Resources
- Chapters
- Conditional Tags
- Topics
- Where to Use Conditional Tags
- The Conditions For
- The Main Page
- The Front Page
- The Administration Panels
- A Single Post Page
- A Single Post, Page, or Attachment
- A Sticky Post
- A Post Type
- A Post Type is Hierarchical
- A Post Type Archive
- Any Page Containing Posts
- A “PAGE” Page
- Testing for Paginated Pages
- Testing for Sub-Pages
- Is a Page Template
- A Category Page
- A Tag Page
- A Taxonomy Page
- A Registered Taxonomy
- An Author Page
- A Multi-author Site
- A Date Page
- Any Archive Page
- A Search Result Page
- A 404 Not Found Page
- A Privacy Policy Page
- An Attachment
- A Single Page, Single Post or Attachment
- A Syndication
- A Trackback
- A Preview
- Has An Excerpt
- Has A Nav Menu Assigned
- Inside The Loop
- Is Sidebar Active
- Part of a Network (Multisite)
- Main Site (Multisite)
- Admin of a Network (Multisite)
- An Active Plugin
- A Child Theme
- Theme supports a feature
- Working Examples
- Single Post
- Check for Multiple Conditionals
- Date Based Differences
- Variable Sidebar Content
- Helpful 404 Page
WordPress if is blog home
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.
Warning: You can only use conditional query tags after the posts_selection action hook in WordPress (the wp action hook is the first one through which you can use these conditionals). For themes, this means the conditional tag will never work properly if you are using it in the body of functions.php, i.e. outside of a function.
However: if you have a reference to the query object (for example, from within the parse_query or pre_get_posts hooks), you can use the WP_Query conditional methods (eg: $query->is_search() )
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
The Front Page
The Blog Page
There is no conditional tag for the blog page. You have to use both is_home() and is_front_page() to detect this page, but those functions can be misused. In fact, a user can define a static page for the homepage, and another page to display the blog. This one will return true with is_home() function, even if it’s not the homepage. Here is what a user can define :
- a default homepage (with the latest posts)
- a static homepage and no blog page
- a static homepage and a blog page
When you use is_home() and is_front_page(), you have to use them in the right order to avoid bugs and to test every user configuration:
The Administration Panels
Attention: The wp-login.php page is not an admin page. To check if this page is displayed, use the admin global variable $pagenow.
The Admin Bar
Note : To display or not this bar, use show_admin_bar(), this function should be called immediately upon plugins_loaded or placed in the theme’s functions.php file.
A Single Post Page
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 Sticky Post
A Post Type is Hierarchical
A Post Type Archive
To turn on post type archives, use ‘has_archive’ => true, when registering the post type.
A Comments Popup
Any Page Containing Posts
A PAGE Page
This section refers to WordPress Pages, not any generic web page from your blog, or in other words to the built in post_type ‘page’.
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 is «about-me», or post_title is «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.
See also is_page() for more snippets, such as is_subpage, is_tree.
Note: There is no function to check if a page is a sub-page. We can get around the problem:
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_page_template() Is a Page Template being used? is_page_template( 'about.php' ) Is Page Template ‘about’ being used?
Note: if the file is in a subdirectory you must include this as well. Meaning that this should be the filepath in relation to your theme as well as the filename, for example page-templates/about.php.
A Category Page
Note: Be sure to check your spelling when testing: «is» and «in» are significantly different.
A Tag Page
A Taxonomy Page (and related)
is_tax
has_term
term_exists
is_taxonomy_hierarchical
taxonomy_exists
An Author Page
A Multi-author Site
A Date Page
Any Archive Page
A Search Result Page
A 404 Not Found Page
A Paged Page
An Attachment
Attachment Is Image
A Local Attachment
A Single Page, a Single Post, an Attachment or Any Other Custom Post Type
Post Type Exists
Is Main Query
Example with the filter hook the_content
If, when WordPress tries to display the content of each post in the Loop or in a single post page, we are in the main query, and not admin side, we add some social buttons (for example).
Example with the action hook pre_get_posts
With pre_get_posts, this is not possible to call directly is_main_query, we should use $query given as a parameter.
A New Day
A Syndication
A Trackback
A Preview
Has An Excerpt
Has A Nav Menu Assigned
Inside The Loop
Is Dynamic SideBar
Is Sidebar Active
Note: To display a sidebar’s content, use dynamic_sidebar( $sidebar ).
Is Widget Active
Note : To be effective this function has to run after widgets have initialized, at action ‘init’ or later, see Action Reference.
Is Blog Installed
Note: The cache will be checked first. If you have a cache plugin, which saves the cache values, then this will work. If you use the default WordPress cache, and the database goes away, then you might have problems.
Right To Left Reading
Part of a Network (Multisite)
Main Site (Multisite)
Admin of a Network (Multisite)
Is User Logged in
Email Exists
Username Exists
An Active Plugin
A Child Theme
Theme supports a feature
Has Post Thumbnail
Script Is In use
This would check if the script named ‘fluidVids.js’ is enqueued. If it is not enqueued, the files are then registered and enqueued.
Is Previewed in the Customizer
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:
Add this custom function to your child themes functions.php file and modify the conditional tag to suit your needs.
You could also use:
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.
When you need display a code or element, in a place that is NOT the home page.
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.
Date-Based Differences
If someone browses our site by date, let’s distinguish posts in different years by using different colors:
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 has an example of using the PHP conditional function isset() in the Writing Friendly Messages section.
Dynamic Menu Highlighting
The Dynamic Menu Highlighting article discusses how to use the conditional tags to enable highlighting of the current page in a menu.
Developer Resources
Determines whether the query is for the blog homepage.
Contents
Description
The blog homepage is the page that shows the time-based blog content of the site.
is_home() is dependent on the site’s «Front page displays» Reading Settings ‘show_on_front’ and ‘page_for_posts’.
If a static page is set for the front page of the site, this function will return true only on the page you set as the «Posts page».
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
See also
Return
bool Whether the query is for the blog homepage.
More Information
History
Since WordPress 2.1, when the static front page functionality was introduced, the blog posts index and site front page have been treated as two different query contexts, with is_home() applying to the blog posts index, and is_front_page() applying to the site front page.
Usage
Be careful not to confuse the two query conditionals:
- On the site front page, is_front_page() will always return true , regardless of whether the site front page displays the blog posts index or a static page.
- On the blog posts index, is_home() will always return true , regardless of whether the blog posts index is displayed on the site front page or a separate page.
Whether is_home() or is_front_page() return true or false depends on the values of certain option values:
- get_option( ‘show_on_front’ ) : returns either ‘posts’ or ‘page’
- get_option( ‘page_on_front’ ) : returns the ID of the static page assigned to the front page
- get_option( ‘page_for_posts’ ) : returns the ID of the static page assigned to the blog posts index (posts page)
When using these query conditionals:
- If ‘posts’ == get_option( ‘show_on_front’ ) :
- On the site front page:
- is_front_page() will return true
- is_home() will return true
- If assigned, WordPress ignores the pages assigned to display the site front page or the blog posts index
- On the site front page:
- If ‘page’ == get_option( ‘show_on_front’ ) :
- On the page assigned to display the site front page:
- is_front_page() will return true
- is_home() will return false
- On the page assigned to display the blog posts index:
- is_front_page() will return false
- is_home() will return true
- On the page assigned to display the site front page:
Notes
is_home() uses the global $wp_query WP_Query object. is_home() isn’t usable before the ‘parse_query’ action.
Developer Resources
Chapters
Conditional Tags
Topics
Conditional Tags can be used in your Template Files in classic themes 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.
The code begins by checking to see if a statement is true or false. If the statement is found to be true, the first set of code is executed. If it’s false, the first set of code is skipped, and the second set of code (after the else) is executed instead.
For example, you could ask if a user is logged in, and then provide a different greeting depending on the result.
Note the close relation these tags have to WordPress Template Hierarchy.
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.php before 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.php that hooks into an action/filter that triggers at a later point
The Conditions For
Listed below are the conditions under which each of the following conditional statements proves to be true. Tags which can accept parameters are noted.
The Main Page
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.
The Front Page
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 pageand
- the Front Page value is the current Page being displayed.
The Administration Panels
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() 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.
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.
Proves true when the Post with Post Slug “beef-stew” is being displayed as a single Post.
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”.
Returns true when the single post being displayed is either post post post or post >
Returns true when the single post being displayed is either the post_name “beef-stew”, post_name “pea-soup” or post_name “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.
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.
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.
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.
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.
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
This section refers to WordPress Pages, not any generic webpage from your blog, or in other words to the built in post_type ‘page’.
When any Page is being displayed.
When Page 42 (ID) is being displayed.
When the Page with a post_title of “About Me And Joe” is being displayed.
When the Page with a post_name (slug) of “about-me” is being displayed.
Returns true when the Pages displayed is either post ID = 42, or post_name is “about-me”, or post_title is “About Me And Joe”.
Returns true when the Pages displayed is either post ID = 42, or post ID = 54, or post ID = 6.
Testing for Paginated Pages
You can use this code to check whether you’re on the nth page in a Post or Page that has been divided into pages using the QuickTag. This can be useful, for example, if you wish to display meta-data only on the first page of a post divided into several pages.
Example 1
Example 2
Testing for Sub-Pages
There is no is_subpage() function, but you can test this with a little code:
Snippet 1
You can create your own is_subpage() function using the code in Snippet 2. Add it to your functions.php file. It tests for a parent page in the same way as Snippet 1, but will return the ID of the page parent if there is one, or false if there isn’t.
Snippet 2
It is advisable to use a function like that in Snippet 2, rather than using the simple test like Snippet 1, if you plan to test for sub-pages frequently.
To test if the parent of a page is a specific page, for instance “About” (page id pid 2 by default), we can use the tests in Snippet 3. These tests check to see if we are looking at the page in question, as well as if we are looking at any child pages. This is useful for setting variables specific to different sections of a web site, so a different banner image, or a different heading.
Snippet 3
Snippet 4 is a function that allows you to carry out the tests above more easily. This function will return true if we are looking at the page in question (so “About”) or one of its sub pages (so a page with a parent with ID “2”).
Snippet 4
Add Snippet 4 to your functions.php file, and call is_tree( ‘id’ ) to see if the current page is the page, or is a sub page of the page. In Snippet 3, is_tree( ‘2’ ) would replace “is_page( ‘about’ ) || ‘2’ == $post->post_parent” inside the first if tag.
Note that if you have more than one level of pages the parent page is the one directly above and not the one at the very top of the hierarchy.
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’ 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 filepath 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.
When the archive page for Category 9 is being displayed.
When the archive page for the Category with Name “Stinky Cheeses” is being displayed.
When the archive page for the Category with Category Slug “blue-cheese” is being displayed.
Returns true when the category of posts being displayed is either term_ID 9, or slug “blue-cheese”, or name “Stinky Cheeses”.
Returns true if the current post is in the specified category id.
Returns true if the current post is in either category 1, 2, or 3.
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”.
A Tag Page
When any Tag archive page is being displayed.
When the archive page for tag with the slug of ‘mild’ is being displayed.
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.
When the current post has the tag ‘mild’.
When the current post has any of the tags in the array.
A Taxonomy Page
When any Taxonomy archive page is being displayed.
When a Taxonomy archive page for the flavor taxonomy is being displayed.
When the archive page for the flavor taxonomy with the slug of ‘mild’ is being displayed.
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.
When the current post has the term ‘green’ from taxonomy ‘color’.
When the current post has any of the terms in the array.
A Registered Taxonomy
An Author Page
When any Author page is being displayed.
When the archive page for Author number (ID) 4 is being displayed.
When the archive page for the Author with Nickname “Vivian” is being displayed.
When the archive page for the Author with Nicename “john-jones” is being displayed.
When the archive page for the author is either user ID 4, or user_nicename “john-jones”, or nickname “Vivian”.
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.
A Privacy Policy Page
When the Privacy Policy page is being displayed.
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() .
True when viewing a post of the Custom Post Types 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.
Other use
When you need to hide the auto displayed excerpt and only display your post’s excerpts.
Replace auto excerpt for a text or code.
Has A Nav Menu Assigned
Whether a registered nav menu location has a menu assigned
Returns: assigned(true) or not(false)
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.
An Active Plugin
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:
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.
When you need display a code or element, in a place that is NOT the home page.
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.
Date Based Differences
If someone browses our site by date, let’s distinguish posts in different years by using different colors:
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 articleh as an example of using the PHP conditional function isset() in the Writing Friendly Messages section.