{"id":138451,"date":"2019-02-05T08:15:02","date_gmt":"2019-02-05T14:15:02","guid":{"rendered":"https:\/\/wpshout.com\/?p=7587"},"modified":"2019-02-05T09:14:32","modified_gmt":"2019-02-05T09:14:32","slug":"wordpress-conditional-tags","status":"publish","type":"post","link":"https:\/\/wpshout.com\/wordpress-conditional-tags\/","title":{"rendered":"How to Use WordPress Conditional Tags"},"content":{"rendered":"
One of the most straightforward, dependable, and useful things in WordPress is what the Codex calls “Conditional Tags.”<\/a> In her interview for Up and Running<\/em><\/a>, Helen Hou-Sand\u00ed said WordPress conditional tags were her favorite WordPress functions for their simplicity and ease of use \u2014 they read just like English. We agree with her on that. They’re great. To explain them to you in more depth, we’re sharing a chapter of Up and Running<\/em> here on WPShout. Let’s get to business understanding conditional tags in WordPress!<\/p>\n\n\t\t One of the most straightforward, dependable, and useful things in WordPress is “conditional tags.” Several of our interviewees mentioned conditional tags being their favorite WordPress functions for their simplicity and ease of use\u2014they read just like English\u2014and we’re pretty much on board.<\/p>\n Here, we’ll discuss the workings of conditional tags, including a few subtle points; list of some of the most useful tags, as well as some of the most confusing; and end with a few examples.<\/p>\n Conditional tags are WordPress functions that return boolean values when called, based on different criteria.<\/p><\/blockquote>\n<\/div>\n Conditional tags are WordPress functions that return a boolean value\u2014either Conditional tags can be called from anywhere in WordPress: themes, plugins, you name it. They’re really useful for making yes-or-no type “conditional” decisions when you need to.<\/p>\n For example, if we write something like Note the syntax of our call to WordPress has over 50 conditional tags. We won’t list them all here, but we will call out a few that are either particularly useful or particularly likely to cause confusion (or both).<\/p>\n<\/div>\n That’s just a partial list of the conditional tags that start with Many conditional tags that evaluate the current post\u2014such as Depending on the function, this attribute can check for the post’s ID, post type slug, or title. Here are a few examples:<\/p>\n There are infinite ways to combine the 50+ conditional tags WordPress supplies to do interesting things. Here are a few common use cases.<\/p>\n<\/div>\n Conditional tags are very commonly used in WordPress theme development. Sometimes one element in a template hierarchy file is showing up in a few situations where you don’t want it.<\/p>\n<\/div>\n For example, let’s say we don’t want to display the date on pages, but we do on all other post types. Maybe Pages and Posts display identically in all other respects\u2014so we don’t want to go to the trouble of creating a We can do:<\/p>\n You could build the template hierarchy’s decision-tree yourself using conditional tags.<\/p><\/blockquote>\n<\/div>\n Taking the example above to its extreme: you can use WordPress conditional tags as a substitute for the entire WordPress template hierarchy! You could just have your whole theme in To actually structure your theme that way is not a great idea, but knowing that it’s possible\u2014and the equivalencies between conditional tags and the template hierarchy\u2014should help you better understand both. For a more visual take on the subject, check this diagram (one of our favorites): https:\/\/wphierarchy.com\/<\/a>.<\/p>\n Conditional tags make it easy to enqueue scripts and styles only on pages where they’re needed.<\/p><\/blockquote>\n<\/div>\n Sometimes, you may want a custom page on your website which looks and behaves very uniquely. Perhaps it has a lot of interactive effects, a different look-and-feel, and so on.<\/p>\n To achieve those effects, the page will have lots of custom JavaScript files and CSS stylesheets\u2014which you only<\/em> need for this one page. Loading them across the site will slow every other page down for no good reason.<\/p>\n Fortunately, with conditional tags it’s easy to only enqueue them for one page alone:<\/p>\n Here, we do our enqueuing as normal, but we’ve preceded the Notice that our Forcing our WordPress’s global state has to be set before you can properly use conditional tags.<\/p><\/blockquote>\n<\/div>\n Conditional tags access WordPress’s global state<\/em>\u2014the “way things are” in the background\u2014and that state has to be set before you can properly use them.<\/p>\n As a result, you can’t dependably use WordPress conditionals until the WordPress “factory” process has marked the This means that some commonly used hooks can’t be used simultaneously with conditional tags. Most of the time, this will be a non-issue, but it’s good to know what you can and can’t do, for the rare case in which this knowledge becomes very important.<\/p>\n Conditional tags are a very easy-to-use way to create conditional functionality of all sorts within WordPress. They’re one of WordPress’s gifts to its developers, so enjoy!<\/p>\n<\/div>\n For code that runs One of the most straightforward, dependable, and useful things in WordPress is what the Codex calls “Conditional Tags.” In her interview for Up and Running, Helen Hou-Sand\u00ed said WordPress conditional tags were her favorite WordPress functions for their simplicity and ease of use \u2014 they read just like English. We agree with her on that.… Read More »How to Use WordPress Conditional Tags<\/span><\/a><\/p>\n","protected":false},"author":98,"featured_media":7598,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_stopmodifiedupdate":false,"_modified_date":"","neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","neve_meta_reading_time":"","_themeisle_gutenberg_block_has_review":false,"footnotes":""},"categories":[8384],"tags":[8385,8391],"hashtags":[],"acf":[],"yoast_head":"\nThe Ultimate Way to Learn WordPress Development Concepts<\/h3>\n\t\t\t
Get Up and Running<\/em> Today!<\/a><\/h2>\n\t\t\t
<\/a>\t\t\tUp and Running<\/em> is our complete “learn WordPress development” course. Now in its updated and expanded 3rd Edition, it’s helped hundreds of happy buyers learn WordPress development the fast, smart, and thorough way.<\/p>\n\t\t\t
\nKey Takeaways about WordPress Conditional Tags:<\/h2>\n
\n
true<\/code> or
false<\/code>) based on the result of its check. Combined with PHP
if<\/code>-statements, this allows easy conditional code execution in either plugins or themes.<\/li>\n
How Conditional Tags in WordPress Work<\/h2>\n
true<\/code> or
false<\/code>\u2014when called, based on different criteria. These criteria are usually spelled out in the function names.<\/p>\n
if ( is_sticky_post() ) {}<\/code> in The Loop, the markup inside the
if<\/code>-statement will show up only<\/em> if the current post is sticky\u2014”stuck” to the top of your blog index, a bit like a pinned tweet\u2014and not otherwise:<\/p>\n
<?php \n\/* Environment: We're inside a theme template file that uses The Loop, like home.php or index.php *\/\n\nif ( have_posts() ) :\n\twhile ( have_posts() ) :\n\t\tthe_post(); \n\n\t\t\/\/ (Regular Loop contents will go here)\n\n\t\tif ( is_sticky_post() ) :\n\t\t\t\/\/ This code will execute *only* for sticky posts!\n\t\tendif;\n\n\tendwhile;\nendif;<\/code><\/pre>\n<\/div>\n
is_sticky_post()<\/code>. Most WordPress conditional tags start with
is_<\/code>, because they’re status checks. And since they’re most often linked up with
if()<\/code>-statements, they really do read like English: “If is sticky post, then\u2026”<\/p>\n
A Few WP Conditional Tags Worth Discussing<\/h2>\n
Useful WordPress Conditional Tags<\/h3>\n
\n
is_single()<\/code>: Is true if the page that’s currently rendering is made up of a single post, of any post type except<\/em> Attachment (a media file) or Page.<\/li>\n
is_page()<\/code>: Is true if the item being displayed is a single post of WordPress’s default “Page” post type.<\/li>\n
is_singular()<\/code>: Similar to
is_single()<\/code>, but it’ll also be true when you’re showing a post of type Attachment or Page. Is equivalent to
is_single() || is_page() || is_attachment()<\/code>, where
||<\/code> means “or.”<\/li>\n
is_archive()<\/code>: Is true if the current page is displaying a set of archives, whether for an author, a date range, a tag, category, etc.<\/li>\n
is_main_query()<\/code>: Is true if the current query is the main query for the page. This one’s really useful when you’re trying to hack the post-fetching process at
pre_get_posts<\/code>\u2014though, in that case, you’re not using it globally, but as a method on the query object passed to you from the filter.<\/li>\n<\/ul>\n
Possibly-Confusing Conditional Tags<\/h3>\n
\n
is_admin()<\/code>: Is true when you’re in the administration area<\/em> of a WordPress site. This can be a little confusing, because you might mistakenly believe that it’ll tell you if the current user<\/em> is an Administrator.<\/li>\n
is_dynamic_sidebar()<\/code>: Is true if any of the currently registered sidebars (widget areas) have active widgets. Not quite what you’d expect, from the name.<\/li>\n
is_home()<\/code>: This one’s confusing. You might think that
is_home()<\/code> will tell you if you’re currently rendering the root of your WordPress site, like
http:\/\/example.com<\/code>. This is not exactly the case: WordPress allows you to have either<\/em> a bunch of blog posts at your site’s root, or a “Static Front Page.” If your site has a static front page, your
is_home()<\/code> will be false<\/em> on the site’s homepage (
example.com<\/code>), and true<\/em> on the site’s blog index (
example.com\/blog<\/code>). In sum,
is_home()<\/code> really amounts to
is_blog_posts_index()<\/code>\u2014which, sadly, does not exist.<\/li>\n
is_front_page()<\/code>:
is_front_page()<\/code> is true if you’re on a static front page, or<\/em> on your blog index if<\/em> you’re not using a static front page. In other words, it’ll always<\/em> be true at
example.com<\/code>, whatever that page has on it. As you can see, it’s quite a bit more like “is home?” than
is_home()<\/code> itself.<\/li>\n<\/ul>\n
is_<\/code>. There are also a few that start with
has_<\/code> (and some that start with neither). The canonical list is on the Codex, at https:\/\/codex.wordpress.org\/Conditional_Tags<\/a>.<\/p>\n
Passing Parameters to Conditional Tags in WordPress<\/h2>\n
is_singular()<\/code>,
is_home()<\/code>, and
is_page()<\/code>\u2014have a shared feature: they accept a function parameter that lets you specify an additional attribute that the post must satisfy.<\/p>\n
\n
is_singular( 'movie-review' )<\/code> checks for a single post of type Movie Review.<\/li>\n
is_page( 'Weekly News Roundup' )<\/code> checks for a Page titled “Weekly News Roundup.”<\/li>\n
is_single( 5 )<\/code> checks for a single post with a post ID of 5.<\/li>\n<\/ol>\n
Some Use Cases for WordPress Conditional Tags<\/h2>\n
Using Conditional Tags in Template Files<\/h3>\n
page.php<\/code> that’s 99% the same as the
index.php<\/code> we already have. Instead, we want to keep
index.php<\/code> as our only template file, no matter what post type is displaying.<\/p>\n
\/* Environment: We're inside The Loop, in index.php *\/\n\n<?php if ( ! is_page() ): ?>\n\t<span class=\"meta-date\">\n\t <?php the_date(); ?>\n\t<\/span>\n<?php endif; ?><\/code><\/pre>\n<\/div>\n
A Note on Conditional Tags and the WordPress Template Hierarchy<\/h4>\n
index.php<\/code>, and use conditional tags to make the same decisions that the template hierarchy normally does.<\/p>\n
Enqueuing Special Stylesheets or JavaScript Files<\/h3>\n
\/* Environment: We're in the active theme's functions.php (or perhaps a PHP file in a \"Special Page\" plugin) *\/\n\nfunction wpshout_special_page( ) {\n if ( ! is_page( 'special-page' ) ) {\n \treturn;\n\t}\n\n wp_enqueue_script( \n 'special_js', \n get_stylesheet_directory_uri().'\/special.js'\n );\n wp_enqueue_style( \n 'special_css', \n get_stylesheet_directory_uri().'\/special.css'\n );\n}\nadd_action( 'wp_enqueue_scripts', 'wpshout_special_page' );<\/code><\/pre>\n<\/div>\n
enqueue<\/code> commands with an
if<\/code>-statement: if the current page isn’t our “special page,” the function returns early and our extra scripts and styles aren’t loaded.<\/p>\n
is_page()<\/code> call actually has to pass two<\/em> tests:<\/p>\n
\n
is_page()<\/code> itself checks for.<\/li>\n
special-page<\/code>? To test for this, we passed
'special-page'<\/code> in as a parameter<\/em> for
is_page()<\/code>.<\/li>\n<\/ol>\n
is_page()<\/code> call to satisfy both<\/em> of these conditions is what makes it possible to restrict our scripts and styles only to our one particular page.<\/p>\n
Note on WordPress Conditional Tags and Global State<\/h2>\n
posts_selection<\/code> action. (Here’s a full list of actions marked by WordPress and their order: https:\/\/codex.wordpress.org\/Plugin_API\/Action_Reference#Actions_Run_During_a_Typical_Request<\/a>.)<\/p>\n
setup_theme<\/code>,
init<\/code>,
register_sidebar<\/code>, and
pre_get_posts<\/code> are a few of the more common action hooks for which conditional tags aren’t accessible. However, about half the hooks that WordPress typically fires, and all the template-rendering that it does, happen well after the template tags are loaded, and so they’re fully useful there.<\/p>\n
Wrapping Up<\/h2>\n
<\/div>\n
Summary Limerick<\/h2>\n
if<\/code> something’s true,
\nConditional tags are for you.
\nWith many a test,
\nThis system is_<\/code> best
\nAt dictating what (not?) to do.<\/p>\n<\/div>\nQuiz Time!<\/h2>\n
\n
return<\/code>s:\n
\n
\n
if ( is_single() ) { echo 'Hi!'; }<\/code> will
echo<\/code> “Hi!” if:\n
\n
Answers and Explanations<\/h4>\n
\n
B.<\/code> In other words, when you execute a conditional tag, you get back either
true<\/code> or
false<\/code>.<\/li>\n
C.<\/code> WordPress’s conditional tags generally relate to properties like post type, post title, and author, not to the dimensions of media attachments.<\/li>\n
B.<\/code>
is_single()<\/code>
return<\/code>s
false<\/code>\u2014even when only a single post is being processed\u2014if the post being processed is of post type Page or Attachment. When the post bundle being processed consists of a single post of any other post type (including custom post types, such as “Movie Review”),
is_single()<\/code>
return<\/code>s
true<\/code>.<\/li>\n<\/ol>\n<\/div>\n<\/div>\n\n\t\t
Seem like a good way to learn WordPress development?<\/h3>\n\t\t\t
Get Up and Running<\/em> Today!<\/a><\/h2>\n\t\t\t
<\/a>\t\t\tYou just read a selection from Up and Running<\/em>, our complete “learn WordPress development” course. Now in its updated and expanded 3rd Edition, it’s helped hundreds of happy buyers learn WordPress development the fast, smart, and thorough way.<\/p>\n\t\t\t