SlideShare a Scribd company logo
1 of 30
Demystifying Conditional Tags
Pittsburgh WordPress
Designers & Developers Meetup
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.
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
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.
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()
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.
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().
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($)
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.
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”.
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
Step 1: Determine The
Appropriate Template File For
Using The Conditional Tag.
Step 2: Determine Where In The
Template File You Want The Text
Block To Appear When The
Conditional Tag Is Evaluated.
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.
Step 3: Insert the Conditional Tag
in the appropriate area you have
chosen
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]
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]
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.
LET’S TRY IT!
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?
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($)
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?
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($)
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?
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($)
POP QUIZ!
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
Quiz 2
What do Conditional Tags evaluate to?
1. YES / NO
2. TRUE / FALSE
3. MET / NOT MET
4. TRUE / NULL
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
Quiz 4
What PHP statement can be used with
some Conditional Tags?
1. Yes statement
2. True statement
3. Then statement
4. If statement

More Related Content

What's hot

Joomla! Day Chicago 2011 - Templating the right way - Jonathan Shroyer
Joomla! Day Chicago 2011 - Templating the right way - Jonathan ShroyerJoomla! Day Chicago 2011 - Templating the right way - Jonathan Shroyer
Joomla! Day Chicago 2011 - Templating the right way - Jonathan ShroyerSteven Pignataro
 
Modular HTML, CSS, & JS Workshop
Modular HTML, CSS, & JS WorkshopModular HTML, CSS, & JS Workshop
Modular HTML, CSS, & JS WorkshopShay Howe
 
Arizona WP - Building a WordPress Theme
Arizona WP - Building a WordPress ThemeArizona WP - Building a WordPress Theme
Arizona WP - Building a WordPress Themecertainstrings
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme EnlightenmentAmanda Giles
 
Introduction to Custom WordPress Themeing
Introduction to Custom WordPress ThemeingIntroduction to Custom WordPress Themeing
Introduction to Custom WordPress ThemeingJamie Schmid
 
WordCamp Detroit 2010 Wordpress Theme Hacks
WordCamp Detroit 2010 Wordpress Theme HacksWordCamp Detroit 2010 Wordpress Theme Hacks
WordCamp Detroit 2010 Wordpress Theme HacksJohn Pratt
 
Creating Customizable Widgets for Unpredictable Needs
Creating Customizable Widgets for Unpredictable NeedsCreating Customizable Widgets for Unpredictable Needs
Creating Customizable Widgets for Unpredictable NeedsAmanda Giles
 
Shortcodes vs Widgets: Which one and how?
Shortcodes vs Widgets: Which one and how?Shortcodes vs Widgets: Which one and how?
Shortcodes vs Widgets: Which one and how?Amanda Giles
 
Dev Theming
Dev ThemingDev Theming
Dev Themingbeedragon
 
Web 102 INtro to CSS
Web 102  INtro to CSSWeb 102  INtro to CSS
Web 102 INtro to CSSHawkman Academy
 
HTML and CSS crash course!
HTML and CSS crash course!HTML and CSS crash course!
HTML and CSS crash course!Ana Cidre
 
Html css crash course may 11th, atlanta
Html css crash course may 11th, atlantaHtml css crash course may 11th, atlanta
Html css crash course may 11th, atlantaThinkful
 
Thinkful - Frontend Crash Course - Intro to HTML/CSS
Thinkful - Frontend Crash Course - Intro to HTML/CSSThinkful - Frontend Crash Course - Intro to HTML/CSS
Thinkful - Frontend Crash Course - Intro to HTML/CSSTJ Stalcup
 
NewBCamp09: Turning your design into a WordPress Theme
NewBCamp09: Turning your design into a WordPress ThemeNewBCamp09: Turning your design into a WordPress Theme
NewBCamp09: Turning your design into a WordPress ThemeAdam Darowski
 
How does get template part works in twenty ten theme
How does get template part works in twenty ten themeHow does get template part works in twenty ten theme
How does get template part works in twenty ten thememohd rozani abd ghani
 
HTML/CSS Crash Course (april 4 2017)
HTML/CSS Crash Course (april 4 2017)HTML/CSS Crash Course (april 4 2017)
HTML/CSS Crash Course (april 4 2017)Daniel Friedman
 

What's hot (20)

Joomla! Day Chicago 2011 - Templating the right way - Jonathan Shroyer
Joomla! Day Chicago 2011 - Templating the right way - Jonathan ShroyerJoomla! Day Chicago 2011 - Templating the right way - Jonathan Shroyer
Joomla! Day Chicago 2011 - Templating the right way - Jonathan Shroyer
 
Modular HTML, CSS, & JS Workshop
Modular HTML, CSS, & JS WorkshopModular HTML, CSS, & JS Workshop
Modular HTML, CSS, & JS Workshop
 
Arizona WP - Building a WordPress Theme
Arizona WP - Building a WordPress ThemeArizona WP - Building a WordPress Theme
Arizona WP - Building a WordPress Theme
 
Rebrand WordPress Admin
Rebrand WordPress AdminRebrand WordPress Admin
Rebrand WordPress Admin
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme Enlightenment
 
Html intro
Html introHtml intro
Html intro
 
Introduction to Custom WordPress Themeing
Introduction to Custom WordPress ThemeingIntroduction to Custom WordPress Themeing
Introduction to Custom WordPress Themeing
 
WordCamp Detroit 2010 Wordpress Theme Hacks
WordCamp Detroit 2010 Wordpress Theme HacksWordCamp Detroit 2010 Wordpress Theme Hacks
WordCamp Detroit 2010 Wordpress Theme Hacks
 
Creating Customizable Widgets for Unpredictable Needs
Creating Customizable Widgets for Unpredictable NeedsCreating Customizable Widgets for Unpredictable Needs
Creating Customizable Widgets for Unpredictable Needs
 
Shortcodes vs Widgets: Which one and how?
Shortcodes vs Widgets: Which one and how?Shortcodes vs Widgets: Which one and how?
Shortcodes vs Widgets: Which one and how?
 
Dev Theming
Dev ThemingDev Theming
Dev Theming
 
Web 102 INtro to CSS
Web 102  INtro to CSSWeb 102  INtro to CSS
Web 102 INtro to CSS
 
HTML and CSS crash course!
HTML and CSS crash course!HTML and CSS crash course!
HTML and CSS crash course!
 
Html css crash course may 11th, atlanta
Html css crash course may 11th, atlantaHtml css crash course may 11th, atlanta
Html css crash course may 11th, atlanta
 
HTML News Packages Lesson
HTML News Packages LessonHTML News Packages Lesson
HTML News Packages Lesson
 
Jquery News Packages
Jquery News PackagesJquery News Packages
Jquery News Packages
 
Thinkful - Frontend Crash Course - Intro to HTML/CSS
Thinkful - Frontend Crash Course - Intro to HTML/CSSThinkful - Frontend Crash Course - Intro to HTML/CSS
Thinkful - Frontend Crash Course - Intro to HTML/CSS
 
NewBCamp09: Turning your design into a WordPress Theme
NewBCamp09: Turning your design into a WordPress ThemeNewBCamp09: Turning your design into a WordPress Theme
NewBCamp09: Turning your design into a WordPress Theme
 
How does get template part works in twenty ten theme
How does get template part works in twenty ten themeHow does get template part works in twenty ten theme
How does get template part works in twenty ten theme
 
HTML/CSS Crash Course (april 4 2017)
HTML/CSS Crash Course (april 4 2017)HTML/CSS Crash Course (april 4 2017)
HTML/CSS Crash Course (april 4 2017)
 

Similar to Demystifying WordPress Conditional Tags

Wordpress theme development
Wordpress theme developmentWordpress theme development
Wordpress theme developmentNaeem Junejo
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017Amanda Giles
 
The WordPress University 2012
The WordPress University 2012The WordPress University 2012
The WordPress University 2012Stephanie Leary
 
WordPress Theme Design and Development Workshop - Day 2
WordPress Theme Design and Development Workshop - Day 2WordPress Theme Design and Development Workshop - Day 2
WordPress Theme Design and Development Workshop - Day 2Mizanur Rahaman Mizan
 
Building the basics (WordPress Ottawa 2014)
Building the basics (WordPress Ottawa 2014)Building the basics (WordPress Ottawa 2014)
Building the basics (WordPress Ottawa 2014)christopherfross
 
HTML Foundations, part 1
HTML Foundations, part 1HTML Foundations, part 1
HTML Foundations, part 1Shawn Calvert
 
Css types internal, external and inline (1)
Css types internal, external and inline (1)Css types internal, external and inline (1)
Css types internal, external and inline (1)Webtech Learning
 
Troubleshooting Technical SEO Problems - Patrick Stox - Raleigh SEO Meetup
Troubleshooting Technical SEO Problems - Patrick Stox - Raleigh SEO MeetupTroubleshooting Technical SEO Problems - Patrick Stox - Raleigh SEO Meetup
Troubleshooting Technical SEO Problems - Patrick Stox - Raleigh SEO Meetuppatrickstox
 
Intro to html, css & sass
Intro to html, css & sassIntro to html, css & sass
Intro to html, css & sassSean Wolfe
 
What's New in WordPress 3.0 (for developers)
What's New in WordPress 3.0 (for developers)What's New in WordPress 3.0 (for developers)
What's New in WordPress 3.0 (for developers)Stephanie Leary
 
seo - on page - part ii - meta &amp; url structure - snippet management
seo - on page - part ii - meta &amp; url structure - snippet managementseo - on page - part ii - meta &amp; url structure - snippet management
seo - on page - part ii - meta &amp; url structure - snippet managementDigipro India
 
Stepping Into Custom Post Types
Stepping Into Custom Post TypesStepping Into Custom Post Types
Stepping Into Custom Post TypesK.Adam White
 
WordPress theme setting page
WordPress theme setting pageWordPress theme setting page
WordPress theme setting pageNaeem Junejo
 
Seven deadly theming sins
Seven deadly theming sinsSeven deadly theming sins
Seven deadly theming sinsGeorge Stephanis
 
Content-Driven WordPress Development - WordCamp Omaha 2014
Content-Driven WordPress Development - WordCamp Omaha 2014Content-Driven WordPress Development - WordCamp Omaha 2014
Content-Driven WordPress Development - WordCamp Omaha 2014Stephanie Eckles
 
Crash Course in Theme Surgery
Crash Course in Theme SurgeryCrash Course in Theme Surgery
Crash Course in Theme SurgeryRational Frank
 
Fixing common problems with SEO by Steve Mortiboy
Fixing common problems with SEO by Steve MortiboyFixing common problems with SEO by Steve Mortiboy
Fixing common problems with SEO by Steve MortiboySteve Mortiboy
 
Advance Blogging.pptx
Advance Blogging.pptxAdvance Blogging.pptx
Advance Blogging.pptxFahimDB
 

Similar to Demystifying WordPress Conditional Tags (20)

Wordpress theme development
Wordpress theme developmentWordpress theme development
Wordpress theme development
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017
 
The WordPress University 2012
The WordPress University 2012The WordPress University 2012
The WordPress University 2012
 
Theme Development from the Coding End
Theme Development from the Coding EndTheme Development from the Coding End
Theme Development from the Coding End
 
WordPress Theme Design and Development Workshop - Day 2
WordPress Theme Design and Development Workshop - Day 2WordPress Theme Design and Development Workshop - Day 2
WordPress Theme Design and Development Workshop - Day 2
 
Building the basics (WordPress Ottawa 2014)
Building the basics (WordPress Ottawa 2014)Building the basics (WordPress Ottawa 2014)
Building the basics (WordPress Ottawa 2014)
 
HTML Foundations, part 1
HTML Foundations, part 1HTML Foundations, part 1
HTML Foundations, part 1
 
Css types internal, external and inline (1)
Css types internal, external and inline (1)Css types internal, external and inline (1)
Css types internal, external and inline (1)
 
Troubleshooting Technical SEO Problems - Patrick Stox - Raleigh SEO Meetup
Troubleshooting Technical SEO Problems - Patrick Stox - Raleigh SEO MeetupTroubleshooting Technical SEO Problems - Patrick Stox - Raleigh SEO Meetup
Troubleshooting Technical SEO Problems - Patrick Stox - Raleigh SEO Meetup
 
Intro to html, css & sass
Intro to html, css & sassIntro to html, css & sass
Intro to html, css & sass
 
What's New in WordPress 3.0 (for developers)
What's New in WordPress 3.0 (for developers)What's New in WordPress 3.0 (for developers)
What's New in WordPress 3.0 (for developers)
 
seo - on page - part ii - meta &amp; url structure - snippet management
seo - on page - part ii - meta &amp; url structure - snippet managementseo - on page - part ii - meta &amp; url structure - snippet management
seo - on page - part ii - meta &amp; url structure - snippet management
 
Stepping Into Custom Post Types
Stepping Into Custom Post TypesStepping Into Custom Post Types
Stepping Into Custom Post Types
 
WordPress theme setting page
WordPress theme setting pageWordPress theme setting page
WordPress theme setting page
 
Seven deadly theming sins
Seven deadly theming sinsSeven deadly theming sins
Seven deadly theming sins
 
Content-Driven WordPress Development - WordCamp Omaha 2014
Content-Driven WordPress Development - WordCamp Omaha 2014Content-Driven WordPress Development - WordCamp Omaha 2014
Content-Driven WordPress Development - WordCamp Omaha 2014
 
Crash Course in Theme Surgery
Crash Course in Theme SurgeryCrash Course in Theme Surgery
Crash Course in Theme Surgery
 
Fixing common problems with SEO by Steve Mortiboy
Fixing common problems with SEO by Steve MortiboyFixing common problems with SEO by Steve Mortiboy
Fixing common problems with SEO by Steve Mortiboy
 
Wordpress(css,php,js,ajax)
Wordpress(css,php,js,ajax)Wordpress(css,php,js,ajax)
Wordpress(css,php,js,ajax)
 
Advance Blogging.pptx
Advance Blogging.pptxAdvance Blogging.pptx
Advance Blogging.pptx
 

Recently uploaded

Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 

Recently uploaded (20)

Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 

Demystifying WordPress Conditional Tags

  • 1. Demystifying Conditional Tags Pittsburgh WordPress Designers & Developers Meetup
  • 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().
  • 8. 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($)
  • 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?
  • 21. 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($)
  • 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?
  • 23. 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($)
  • 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?
  • 25. 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($)
  • 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