Conditional Love
Using WordPress Conditional Tags to
Write More Effective Themes
WordCamp Jacksonville 2016
@cwpnolen @emagineusa#wcjax
@cwpnolen @emagineusa#wcjax
Who Am I?
Christian Nolen
Technical Director for emagine
WordPress Developer
@cwpnolen
@cwpnolen @emagineusa#wcjax
We’re Hiring
http://www.emagine.com/careers/
@cwpnolen @emagineusa#wcjax
@cwpnolen @emagineusa#wcjax
All Developers 

are Lazy
@cwpnolen @emagineusa#wcjax
@*#&-%*!
@cwpnolen @emagineusa#wcjax
@cwpnolen @emagineusa#wcjax
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
CONDITIONAL TAGS
@cwpnolen @emagineusa#wcjax
1. the state in which something exists
CONDITION
(kən-ˈdi-shən)
noun
2. a state of being
@cwpnolen @emagineusa#wcjax
I JUST DROPPED IN
To See What Condition My Condition Was In
WordPress
@cwpnolen @emagineusa#wcjax
@cwpnolen @emagineusa#wcjax
1. the state in which something exists
CONDITION
(kən-ˈdi-shən)
noun
2. a state of being
@cwpnolen @emagineusa#wcjax
Are You Hungry?
@cwpnolen @emagineusa#wcjax
Are You Tired?
@cwpnolen @emagineusa#wcjax
Are You Sad?
@cwpnolen @emagineusa#wcjax
Are You Tired?

or ( || )

Is it Morning?
@cwpnolen @emagineusa#wcjax
Are You Drunk?
and (&&)
Do You Feel Sick?
@cwpnolen @emagineusa#wcjax
allow you to ask one or many yes/no
(true/false) questions to WordPress
for the purpose of changing content,
presentation or functionality
CONDITIONAL TAGS
@cwpnolen @emagineusa#wcjax
Template Hierarchy
https://developer.wordpress.org/themes/basics/template-hierarchy/
@cwpnolen @emagineusa#wcjax
Title: About Us

Slug: about-us

Post ID: 28
Example Page
@cwpnolen @emagineusa#wcjax
tmpl-about-us.php
page-about-us.php
page-28.php
page.php
singular.php
index.php
@cwpnolen @emagineusa#wcjax
Why?
@cwpnolen @emagineusa#wcjax
We don’t want this either
@cwpnolen @emagineusa#wcjax
Consolidate your templates by 

major functional or design requirements
@cwpnolen @emagineusa#wcjax
Common Usage
@cwpnolen @emagineusa#wcjax
is_home()
is_front_page()
@cwpnolen @emagineusa#wcjax
is_home() / is_front_page()
if ( is_home() ) : 

// Load blog listing template
get_template_part( 'partials', 'blog-posts' ); 

endif;
if ( is_front_page() ) : 

// Load static home page stylesheet and javascript

wp_enqueue_style( 'my-registered—home-css' );
wp_enqueue_script( 'my-registered—home-js' ); 

endif;
@cwpnolen @emagineusa#wcjax
is_category()
Parameters: Category ID, Name or Slug
@cwpnolen @emagineusa#wcjax
is_category()
if ( is_category() ) : 

// Get Category Data
$category = get_category( get_query_var( 'cat' ) ); 

endif;
<?php if ( is_category( 'web-development' ) ) : ?>

<!—- A Geeky Image —->
<img src="http://goo.gl/8rqmrJ" alt="Nerd Image" /> 

<php endif; ?>
@cwpnolen @emagineusa#wcjax
is_active_sidebar()
Parameters: Name or ID
(required)
@cwpnolen @emagineusa#wcjax
is_active_sidebar()
if ( is_active_sidebar( 'call-to-actions' ) ) : 

echo '<aside class="widgets">';
dynamic_sidebar( 'call-to-actions' );

echo '</aside>'; 

endif;
@cwpnolen @emagineusa#wcjax
is_single()
Parameters: Post ID, Title or Slug
@cwpnolen @emagineusa#wcjax
is_single()
if ( is_single( '28' ) ) : 

// Awesome functionality 

// being conditionally loaded… poorly

endif;
@cwpnolen @emagineusa#wcjax
More Common Uses
has_post_thumbnail()
is_page()
is_admin()
has_excerpt()
is_author()
is_multisite()
@cwpnolen @emagineusa#wcjax
Real World Applications
@cwpnolen @emagineusa#wcjax
All Pages on Your Client Site
Landing Page
@cwpnolen @emagineusa#wcjax
get_header( 'landing-page' );
@cwpnolen @emagineusa#wcjax
is_page_template()
Parameter: Template Name
@cwpnolen @emagineusa#wcjax
is_page_template()
if ( is_page_template( 'tmpl-landing-page.php' ) ) : 

// Get template partial for Landing Page header
get_template_part( 'partials', 'header-links-lp' );

else :
// Get default header links
get_template_part( 'partials', 'header-links-default' );
endif;
@cwpnolen @emagineusa#wcjax
@cwpnolen @emagineusa#wcjax
@cwpnolen @emagineusa#wcjax
single-{post-type}-{slug}.php
single-{post-type}.php
single.php
singular.php
index.php
@cwpnolen @emagineusa#wcjax
Posts
single-{post-type}-{slug}.php
single-{post-type}.php
single.php
singular.php
index.php
Pages
tmpl-about-us.php
page-about-us.php
page-28.php
page.php
singular.php
index.php
@cwpnolen @emagineusa#wcjax
is_singular()
Parameter: Post Type Slug
@cwpnolen @emagineusa#wcjax
is_singular()
if ( is_single() || is_page() || is_attachment() )
if ( is_singular() )
=
@cwpnolen @emagineusa#wcjax
<article>
<?php
if ( is_singular( 'post' ) ) :
// Get blog date, author and categories

get_template_part( 'partials/blog', 'meta' );
endif;
the_content();
?>
</article>
is_singular()
@cwpnolen @emagineusa#wcjax
is_404()
@cwpnolen @emagineusa#wcjax
while ( have_posts() ) : the_post();
the_content();
endwhile;
if ( is_404() ) :
// The page does not exist
echo 'This isn’t the page you’re looking for.’;
endif;
is_404()
@cwpnolen @emagineusa#wcjax
@cwpnolen @emagineusa#wcjax
Make Your Own 

Conditional Tag
@cwpnolen @emagineusa#wcjax
function is_subpage() {
global $post;
if ( is_page() && $post->post_parent ) :
return true;
else :
return false;
endif;
}
@cwpnolen @emagineusa#wcjax
Extend WordPress
Functionality
@cwpnolen @emagineusa#wcjax
add_filter( 'body_class', 'add_body_classes' );
function add_body_classes( $classes ) {
return $classes;
}
if ( !is_front_page() )
$classes[] = 'interior';
if ( is_active_sidebar( 'call-to-actions' )
$classes[] = 'has-callouts';
if ( is_subpage() )
$classes[] = ‘is-subpage';
@cwpnolen @emagineusa#wcjax
Additional Resources
• https://codex.wordpress.org/Conditional_Tags
• https://developer.wordpress.org/themes/basics/conditional-tags/
• http://code.tutsplus.com/articles/php-for-wordpress-mastering-
conditional-statements-and-tags--wp-22725
• https://docs.woothemes.com/document/conditional-tags/
• https://codex.bbpress.org/bbpress-conditional-tags/
• https://codex.buddypress.org/developer/template-tag-reference/
#is_-functions
@cwpnolen @emagineusa#wcjax
Thank You
Christian Nolen
Technical Director for emagine
@cwpnolen

Conditional Love - Using WordPress Conditional Tags to Write More Effective Themes