30
Demystifying Conditional Tags Pittsburgh WordPress Designers & Developers Meetup

Demystifying WordPress Conditional Tags

Embed Size (px)

Citation preview

Page 1: Demystifying WordPress Conditional Tags

Demystifying Conditional Tags

Pittsburgh WordPress

Designers & Developers Meetup

Page 2: Demystifying WordPress Conditional Tags

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.

Page 3: Demystifying WordPress Conditional Tags

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

Page 4: Demystifying WordPress Conditional Tags

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.

Page 5: Demystifying WordPress Conditional Tags

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()

Page 6: Demystifying WordPress Conditional Tags

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.

Page 7: Demystifying WordPress Conditional Tags

Conditional Tags Examples

• A complete list of conditional tags available for use can be viewed at https://codex.wordpress.org/Conditional_Tags

• 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().

Page 8: Demystifying WordPress Conditional Tags

Conditional Tags (abbreviated list)

• is_home()

• is_front_page()

• is_single()

• is_page()

• is_page_template()

• is_category()

• is_tag()

• has_tag()

• is_author()

• is_archive()

• is_search()

• is_404()

• has_excerpt()

• is_active_sidebar()

• is_child_theme()

• has_post_thumbnail($)

Page 9: Demystifying WordPress Conditional Tags

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.

Page 10: Demystifying WordPress Conditional Tags

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”.

Page 11: Demystifying WordPress Conditional Tags

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

Page 12: Demystifying WordPress Conditional Tags

Step 1: Determine The

Appropriate Template File For

Using The Conditional Tag.

Page 13: Demystifying WordPress Conditional Tags

Step 2: Determine Where In The

Template File You Want The Text

Block To Appear When The

Conditional Tag Is Evaluated.

Page 14: Demystifying WordPress Conditional Tags

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.

Page 15: Demystifying WordPress Conditional Tags

Step 3: Insert the Conditional Tag

in the appropriate area you have

chosen

Page 16: Demystifying WordPress Conditional Tags

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 Cheeseecho "<p>This is my about my favorite cheese!</p>";}[/html]

Page 17: Demystifying WordPress Conditional Tags

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 Cheeseecho "This is my about my favorite cheese!";}

• // Start the Loop.while ( have_posts() ) : the_post();[/html]

Page 18: Demystifying WordPress Conditional Tags

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.

Page 19: Demystifying WordPress Conditional Tags

LET’S TRY IT!

Page 20: Demystifying WordPress Conditional Tags

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?

Page 21: Demystifying WordPress Conditional Tags

Conditional Tags (abbreviated list)

• is_home()

• is_front_page()

• is_single()

• is_page()

• is_page_template()

• is_category()

• is_tag()

• has_tag()

• is_author()

• is_archive()

• is_search()

• is_404()

• has_excerpt()

• is_active_sidebar()

• is_child_theme()

• has_post_thumbnail($)

Page 22: Demystifying WordPress Conditional Tags

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?

Page 23: Demystifying WordPress Conditional Tags

Conditional Tags (abbreviated list)

• is_home()

• is_front_page()

• is_single()

• is_page()

• is_page_template()

• is_category()

• is_tag()

• has_tag()

• is_author()

• is_archive()

• is_search()

• is_404()

• has_excerpt()

• is_active_sidebar()

• is_child_theme()

• has_post_thumbnail($)

Page 24: Demystifying WordPress Conditional Tags

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?

Page 25: Demystifying WordPress Conditional Tags

Conditional Tags (abbreviated list)

• is_home()

• is_front_page()

• is_single()

• is_page()

• is_page_template()

• is_category()

• is_tag()

• has_tag()

• is_author()

• is_archive()

• is_search()

• is_404()

• has_excerpt()

• is_active_sidebar()

• is_child_theme()

• has_post_thumbnail($)

Page 26: Demystifying WordPress Conditional Tags

POP QUIZ!

Page 27: Demystifying WordPress Conditional Tags

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

Page 28: Demystifying WordPress Conditional Tags

Quiz 2

What do Conditional Tags evaluate to?

1. YES / NO

2. TRUE / FALSE

3. MET / NOT MET

4. TRUE / NULL

Page 29: Demystifying WordPress Conditional Tags

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

Page 30: Demystifying WordPress Conditional Tags

Quiz 4

What PHP statement can be used with

some Conditional Tags?

1. Yes statement

2. True statement

3. Then statement

4. If statement