2. What are Conditional Tags?
⢠Conditional tags allow you to display
unique content on a page depending on
the conditions the page matches.
⢠This means you can have a block of
content that only shows up on the home
page and not on other pages.
⢠In this lesson youâll learn how to harness
the power of conditional tags and use
them in your theme templates.
3. Prerequisite Skills
⢠Basic knowledge of PHP and HTML
⢠Basic knowledge of installing and
activating WordPress themes
⢠Understanding of the Template Hierarchy
⢠Ability to edit files with a text editor
4. Welcome to Conditional Tags!
⢠Conditional tags allow theme designers to
pose a question and if that question
evaluates to either TRUE or FALSE,
something will happen.
⢠These tags can be used to generate new
content on a specific post, page, category,
and taxonomy template.
5. Welcome to Conditional Tags!
⢠If the question you pose evaluates to
TRUE, then the content you want to
display will appear.
⢠If it evaluates to FALSE, nothing will
appear in that area.
⢠Use â!â in front of conditional tag to get the
opposite state - !is_single()
6. Conditional Tags examples
⢠is_single() - when any single Post,
attachment or custom Post Type page is
being displayed. This will evaluate to
FALSE for Pages.
⢠is_single( '17' ) - when Post 17 is being
displayed as a single Post.
⢠is_single( 'Irish Stew' ) - when the Post
with Title âIrish Stewâ is being displayed as
a single Post.
7. Conditional Tags Examples
⢠A complete list of conditional tags available
for use can be viewed
at https://codex.wordpress.org/Conditional_T
ags
⢠Some conditional tags function in parallel to a
corresponding theme template file, for
example, the error page template. The
template file 404.php could be used, or
invoking the conditional tag is_404().
9. Purpose of Conditional Tags
In WordPress, specific template files control
how your website displays content. For
example, page.php controls what every
single âpageâ on your website looks like. This
saves time in developing by not having to
code every single page on a website;
however, if you want something different to
happen on a few specific pages, you would
need to use Conditional Tags to accomplish
this.
10. Conditional Tags in Action
⢠Letâs say you are building a cooking website
that shows healthy recipes for families.
⢠Each post would display a new recipe.
⢠These posts could also be categorized into
ingredients such as Ground Beef, Cheddar
Cheese, Milk, etc.
⢠For todayâs example you are going to display
a simple block of text at the top of any single
recipe post if it happens to be in the category
of âBlue Cheeseâ.
11. Purpose of Conditional Tags
⢠display a specific block of text above
displayed posts, but only on the home page
⢠display a block of text on a post that has a
specific category
⢠display a block of text on a post that has one
of several specific categories
⢠display a content section on all posts except
ones tagged with a specific tag
12. Step 1: Determine The
Appropriate Template File For
Using The Conditional Tag.
13. Step 2: Determine Where In The
Template File You Want The Text
Block To Appear When The
Conditional Tag Is Evaluated.
14. Step 2
get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content"
role="main">
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
[/html]
We will want
to insert the
conditional test
after the opening
#content div, but
before the
while (have
posts() ) :
the_post()
statement.
15. Step 3: Insert the Conditional Tag
in the appropriate area you have
chosen
16. Step 3
[html]
// use conditional tag to check if post has
category with slug âblue-cheeseâ
is_category( âblue-cheeseâ) {
// display text when weâre looking at posts in
the category of Blue Cheese
echo "<p>This is my about my favorite
cheese!</p>";
}
[/html]
17. Step 3
⢠get_header(); ?>
⢠<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
⢠<?php
⢠// use conditional tag to check if post has category with
slug âblue-cheeseâ
⢠is_category( âblue-cheeseâ) {
// display text when weâre looking at posts about Blue
Cheese
echo "This is my about my favorite cheese!";
}
⢠// Start the Loop.
while ( have_posts() ) : the_post();
[/html]
18. Summary
⢠Conditional tags are a simple way to check
for specific conditions, as a true or false test.
Using just a few lines, you can check to see if
one or more conditions match, and execute
additional functions. These can be mixed and
matched and used in all template files to
achieve the end result desired.
⢠Conditions vary from the id or tag of a post or
page to the type of content being viewed.
20. Exercise 1
⢠Modify the single post template theme file
to display a block of text after the post
content but before the comments if the
post has ID of 2.
⢠What is the Conditional Tag you will need to
use to accomplish this? If you donât know,
where can you find a list of Conditional Tags?
⢠How do you tell the Conditional Tag to only
display the clock of text if the post has an ID
of 2?
22. Exercise 2
⢠Display a block of text if a Post has a
tag of âBreakfastâ attached to it.
⢠Will you use âisâ or âhasâ in your
Conditional Tag for this exercise?
⢠Which template file will you need to edit to
accomplish this task?
24. Exercise 3
⢠Add a block of content to the footer and
use Conditional Tags to make it display
only on the front page of your site.
⢠What template file should you look for to
add a block of content to your footer?
⢠What Conditional Tag should be used in
this instance?
27. Quiz 1
Which of the following examples would not
be accomplished using Conditional Tags?
1. Display a block of text on three pages with
the ID of 4, 17, and 22
2. Display a different sidebar on the front page
than on inner pages
3. Display your posts on the front page of your
site
4. Display a different title on the âGround Beefâ
category archive page
28. Quiz 2
What do Conditional Tags evaluate to?
1. YES / NO
2. TRUE / FALSE
3. MET / NOT MET
4. TRUE / NULL
29. Quiz 3
What variable(s) can you pass into
the is_single() Conditional Tag?
1. The Post ID is_single( '22' )
2. The Post Title is_single( 'Fudge Brownies' )
3. The Post Slug is_single( 'fudge-brownies' )
4. The Post Array is_single( array( 22, 'fudge-
brownies', 'Fudge Brownies' )
5. All of the above
30. Quiz 4
What PHP statement can be used with
some Conditional Tags?
1. Yes statement
2. True statement
3. Then statement
4. If statement