There dozens and dozens of conditional tags included with WordPress,
and I highly recommend that you browse through them on the Conditional Tags Codex page. I am going to talk about and walk through perhaps a dozen of the most commonly used.
is_home() This is a very common tag, but it is also very commonly misused, or at least misunderstood. You can use this tag to detect whether a visitor is on the blog page or not. As WordPress began as a blogging engine, the main blog page often acted as the site’s landing, or home, page. This tag will check for whatever page you have set as “Page for Posts” in the Reading section of the WordPress settings. It will also return true when on the site’s home page if you have the home page set to “Display Latest Posts”.
if( is_home() ) { echo 'Display this content on the blog page only'; }
is_front_page() This is the tag that will “truly” check whether a visitor is on the home/front page. As its name suggests, is_front_page() will return true when a visitor is looking at the site’s front page, regardless of whether your home page is set to “Display Latest Posts” or to “Static Page”.
if( is_front_page() ) { echo 'Display this content on the front/home page only'; }
is_single() This is used to detect when we are viewing a “single” blog post. For example, if we are viewing the complete details of the default “Hello World” blog post, then this conditional will return true. If you are familiar with theme development, and the WordPress template hierarchy, then this tag is more or less synonymous with the single.php file.
if( is_single() ) { echo 'This displays when viewing a single blog post'; }
Unlike is_home() and is_front_page(), this tag can accept parameters for the post title, slug, ID, or a mixed array of these. For example, if we wanted to only output content for the “Hellow World” post, we can do this:
if( is_single('Hello World') ) { echo 'This displays when viewing a single blog post'; }
Or, if we wanted to output something only for the blog post with an ID of 97, we can do this:
if( is_single(97) ) { echo 'This displays when viewing a single blog post'; }
You might notice that the first example has the post title wrapped in single quotes. This is because “Hello World” is a string, whereas 97 is an integer. If you don’t wrap the single in single or double quotes, you will get a syntax error.
We can also combine both of our examples into one, for when we want to display the content on both Hello World and post ID 97:
if( is_single( array(97, 'Hello World') ) ) { echo 'This displays when viewing the "Hello World" post and the post with an ID of 97'; }
Note, this tag does NOT return true when viewing the complete details/content of a Page.
is_page() As used in our first example, this lets us check if we are viewing a Page item. Look at the example above if you want a simple example. If we want to check if we are on any of the specified pages, we can use an array, like I did in the last example of is_single():
if( is_page( array('my-page-slug', 'Contact') ) ) { echo 'This displays when viewing the "Contact" page and the page with a slug of "my-page-slug"'; }
is_singular() This tag is sort of a combination of is_single() and is_page(). It returns true anytime we are viewing a single blog post, a page, an attachment, or any custom post type. For example, the following will return true if we are on a page, post, attachment page, or custom post type details page:
if( is_singular() ) { echo 'This displays anytime we are viewing a details page'; }
Essentially, is_singular() works for checking if we are viewing the details for anything. One of the great things about is_singular(), is that we can also use it to check if we are on the details page for a specific custom post type, like so:
if( is_singular( 'movies' ) ) { echo 'This displays when viewing a movies detail page'; }
If you more quires contact us
