Conditional Tags can be used to customize your WordPress website when TRUE or FALSE conditions are met.
This introductory example will show you how to display some of the post author’s user information when on an individual post or the author’s archive page.
<?php /* * Conditional Tags * When on an archive or post page display * user description and email address * */ if ( is_author() || is_single() ) { echo '<p><strong>About the Author</strong><br />' . get_the_author_meta( 'description' ) . '</p>' ; echo '<p><a href="mailto:' . get_the_author_meta( 'user_email' ) . '">' . get_the_author_meta( 'user_email' ) . '</a></p>'; } else { /* * Do nothing * */ } ?>
What this code does
When the displayed page is an author archive or a single post the conditions are met and return a value of TRUE. This displays the text About the Author in bold followed by that author’s biographical information and email address from their WordPress user profile.
When the displayed page doesn’t meet the conditional tag’s requirements it returns a value of FALSE and continues to the else statement, in this case it does nothing.
When Conditional tags are useful
Displaying information by using TRUE or FALSE statements allows your site to respond according to specific situations. For instance, placing this code in your Twenty Seventeen child theme’s sidebar allows it to display additional information above the dynamic sidebar.
You will notice a reference to pairings in the screenshot above. This checks the post’s tags and displays a related post using two a WP_Querys.
<?php /* * Conditional Tags * * checks beer type and availability * displays meal pairing */ // When the post is tagged ipa and is in the category on-tap if ( has_tag( 'ipa' ) && in_category( 'on-tap' ) ) { echo '<strong>Pairs well with</strong><br />'; // Display Pork menu items // The Query -- See https://codex.wordpress.org/Class_Reference/WP_Query $the_query = new WP_Query( array( 'tag' => 'pork', ) ); // The Loop - Displays all pork items to go with IPAs if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a><br /><br />'; } /* Restore original Post Data */ wp_reset_postdata(); } else { // no posts found } } /* * Checks food item tag and * displays it's beer pairing * */ if (has_tag( 'pork' ) ) { echo '<strong>Pairs well with</strong><br />'; // Display ipa tagged items // The Query -- See https://codex.wordpress.org/Class_Reference/WP_Query $the_query = new WP_Query( array( 'tag' => 'ipa', ) ); // The Loop - display all ipa tagged posts if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a><br /><br />'; } /* Restore original Post Data */ wp_reset_postdata(); } else { // no posts found } }
Okay, that last one was a lot of code. However, when you put both together the result is a customized sidebar responsive to the page’s content.
Examples of Conditional tags at work
Using both together in the sidebar creates a mixture of results.
This can allow you to customize your website while creating fewer new files making it easier to maintain.
If you’d like to learn more about Conditional Tags.
- Visit the Codex page for Conditional Tags.
- Watch Kathryn Presner’s presentation on Conditional Tags from WordCamp Montreal.