In this series, we're going through one of the fundamental features of WordPress: Conditional Tags. In this third part, we'll continue introducing and reviewing the Conditional Tags. Be sure to check out the previous parts if you haven't yet.
Let's begin!
14. Checking Whether We're on the Front Page: is_front_page()
In WordPress, the "front page" can be set to a static WordPress page or the list of the latest blog posts (Settings > Reading). Either way, the Conditional Tag is_front_page()
returns TRUE
when the front page is being displayed.
Accepted Parameters
This Conditional Tag doesn't accept any parameters.
15. Checking Whether the Post Has a Thumbnail: has_post_thumbnail()
"Featured images" are one of the key parts of the New Post/Page screen. The Conditional Tag has_post_thumbnail()
determines whether there's an assigned featured image to the given post or not.
Accepted Parameters
This Conditional Tag has only one parameter:
-
$post_id
(integer, optional): The post ID. (Default: Current post ID)
Usage Example for has_post_thumbnail()
Let's say you're developing a theme and in it, every single blog post needs to have a "featured image", so you want to display a "default image" if no featured image is set. Here's what you do:
<?php function mytheme_featured_image( $class = '' ) { global $post; $post_title = get_the_title(); if ( has_post_thumbnail( $post->ID ) ) { $featured_image = get_the_post_thumbnail( $post->ID, 'thumbnail', array( 'alt' => esc_attr( $post_title ), 'class' => $class ) ); } else { $featured_image = '<img src="' . get_stylesheet_directory_uri() . '/assets/default-thumb.jpg" alt="' . esc_attr( $post_title ) . '" class="default-thumb ' . $class . '" />'; } return $featured_image; } /* * Usage (inside the Loop): * echo mytheme_featured_image( 'my-custom-class' ); */ ?>
16. Checking Whether the Theme Is Using the "Comments Popup": is_comments_popup()
You shouldn't judge anyone who uses a 10-year-old theme—or loves retro. If you're developing a plugin, you need to take everything into account, and that includes themes that use the comments popup window. To determine this, you can use the Conditional Tag is_comments_popup()
.
Accepted Parameters
This Conditional Tag doesn't accept any parameters.
17. Checking Whether the Page Is a 404 Error Page: is_404()
The "Not Found" error pages are usually the ones we hate to see in our websites, and thus we don't really care how they look. But when you use these error pages correctly, you can turn them into useful pages that inform your users or help them navigate. The Conditional Tag is_404()
helps us determine whether a 404 error is being displayed to the user.
Accepted Parameters
This Conditional Tag doesn't accept any parameters.
Usage Example for is_404()
Let's say you're developing a plugin that logs broken internal links, and you want your function to run each time a 404 error page is viewed. Here's what you do:
<?php add_filter( 'template_redirect', 'my_plugin_check_404_pages' ); function my_plugin_check_404_pages() { if ( is_404() ) { my_plugin_404_logger_function(); } } ?>
18. Checking Whether the Given Taxonomy Exists: taxonomy_exists()
If you ever need to check whether a custom taxonomy is already registered, you can use the taxonomy_exists()
Conditional Tag to make your code determine it.
Accepted Parameters
This Conditional Tag has only one parameter:
-
$taxonomy
(string, required): Taxonomy's name. (Default: None)
19. Checking Whether the Page Is the "Search Results" Page: is_search()
While being a little underrated, "Search Results" pages are an important part of WordPress websites. And if you're developing a plugin or a theme, you can detect these pages with the help of the is_search()
Conditional Tag.
Accepted Parameters
This Conditional Tag doesn't accept any parameters.
Usage Example for is_search()
Let's say you want to include a link for a Google search with the same terms. Here's what you do:
<?php if ( is_search() ) { $search_query = get_search_query(); echo '<div class="google-search"><a href="https://www.google.com.tr/search?q=' . $search_query . '">' . __( 'Search with Google', 'translation-domain' ) . '</a>'; } ?>
20. Checking Whether the Page Is a "Tag Archives" Page: is_tag()
Want to treat tag archives differently? The Conditional Tag is_tag()
can help you with that. Want to treat specific tags differently? Just pass a tag name, slug or ID (or an array of those) as the parameter!
Accepted Parameters
This Conditional Tag has only one parameter:
-
$tag
(array/string, optional): Tag's ID, name, slug or an array of those. (Default: None)
21. Checking Whether the Post Has a Custom Excerpt: has_excerpt()
There are two kinds of "excerpts" in WordPress posts: If you want to write one yourself, it's called a "custom excerpt", and if you don't, an "automatic excerpt" is generated (by default, it's the first 55 words of the post). The has_excerpt()
Conditional Tag checks whether the user set a custom excerpt for the given post.
Accepted Parameters
This Conditional Tag has only one parameter:
-
$post_id
(integer, optional): The post ID. (Default: Current post ID)
Usage Example for has_excerpt()
Let's say you're making a theme and you want to display custom excerpts in the homepage, but you don't want to display automatic excerpts. Here's what you do:
<?php if ( has_excerpt() ) { the_excerpt(); } ?>
22. Checking Whether It's the Main Query: is_main_query()
WordPress uses the WP_Query
class to list posts—whether it be just a list of post titles or the index of full posts in an archive page. A number of functions use the WP_Query
class, and is_main_query()
is one of them. This Conditional Tag detects whether the query is not a "secondary query", but the "main query".
Accepted Parameters
This Conditional Tag doesn't accept any parameters.
23. Checking Whether the Post Has the Given Tag: has_tag()
Sometimes, you may need to check if a post has some tag to make the post (or posts) act differently than others. To achieve this, you can use has_tag()
to check whether a post is tagged with the tag you specify. (Note: It allows you to specify more than one tag to look for.)
Accepted Parameters
This Conditional Tag has two parameters:
-
$tag
(array/string, optional): Tag's name, ID, slug, or an array of those. (Default: None) -
$post
(object, optional): Post to check. (Default: Current post)
Usage Example for has_tag()
Let's say you have "badges" for your blog posts (like "NEW", "FEATURED" and "OBSOLETE") that are going to be activated by using corresponding tags, and you want to echo the images inside posts. Here's what you do:
<?php if ( has_tag( 'badge-new' ) ) { echo '<div class="post-content badge-new">'; } else if ( has_tag( 'badge-featured' ) ) { echo '<div class="post-content badge-featured">'; } else if ( has_tag( 'badge-obsolete' ) ) { echo '<div class="post-content badge-obsolete">'; } else { echo '<div class="post-content">'; } // Post content. echo '</div>'; ?>
24. Checking Whether the Blog Is Installed (?): is_blog_installed()
This particular Conditional Tag returns TRUE
if WordPress is installed. I'm including this Conditional Tag just for reference, because technically it can't be useful for plugin or theme developers—it could be used in some external WordPress tool, maybe.
Accepted Parameters
This Conditional Tag doesn't accept any parameters.
25. Checking Whether the User Is a "Super Admin": is_super_admin()
In a Multisite network, there is a "super admin" which can administer all the sites. To detect if the user is a "super admin" (or a regular admin in regular WordPress installations), you can use the is_super_admin()
Conditional Tag.
Accepted Parameters
This Conditional Tag has only one parameter:
-
$user_id
(integer, optional): User's ID. (Default: Current user)
Usage Example for is_super_admin()
Let's say you don't like the "Howdy" greeting and you want to change it, but your users like it and want it to stay. In that case, you need a solution to apply to you only. Here's what you do:
<?php // Source: http://www.paulund.co.uk/change-the-wordpress-howdy-text add_filter( 'admin_bar_menu', 'replace_howdy' ); function replace_howdy( $wp_admin_bar ) { $my_account = $wp_admin_bar->get_node( 'my-account' ); $newtitle = __( 'Hi boss!', 'translation-domain' ); $wp_admin_bar->add_node( array( 'id' => 'my-account', 'title' => $newtitle ) ); return $wp_admin_bar; } ?>
26. Checking Whether the Page Is a "Page" Page: is_page()
In WordPress, "pages" are one of the five built-in post types along with posts, revisions, attachments and navigation menus. And if you want to detect a certain page (or any page in general), you can use the Conditional Tag is_page()
.
Accepted Parameters
This Conditional Tag has only one parameter:
-
$page
(array/string, optional): Page ID, title, slug or an array of those. (Default: None)
Conclusion
In this part, we reviewed another batch of the 65 documented Conditional Tags in WordPress. In the next parts, we're going to go through the remaining 39. If you have any questions or comments, shoot them below—and if you liked this article, don't forget to share it!
See you in the next part!
Comments