• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to user navigation

PopWP

WordPress and Genesis Tutorials

  • Get Started
  • About
  • Archive
  • Services
  • Membership
  • My Account

Conditional Tags in WordPress for Genesis Framework

Last Updated on September 28, 2019 Favorited: 1 times

Conditional Tags are functions used in WordPress to display a specific content or to perform actions based on whether a certain condition is true or false.

The conditional tags can be used with both themes and plugins and so this is recommended to take a look before making a theme or plugin or adding content using Genesis hooks.

A Conditional tag gives you a full control over your content and provides you a facility to show them on specific post or page including custom post, archive pages, taxonomy and more.

Using WordPress Conditional Tags in Genesis with the help of WordPress hooks and Genesis hooks, you can create and control infinite contents on your site.

Remember: Most of the Page Template in WordPress work itself as a conditional value. Like single.php file work for single posts. Means you can use this template to enqueue some file, to do some function on single posts.

The Format and Syntax of a Conditional Tag

There is no limit of the numbers of conditional tags available since one can easily create their own, but all the conditional tag will follow the same basic appearance and syntax. Conditional tags are boolean function, meaning it returns either TRUE or FALSE. The basic format is:-

Sample 1.

if ( some condition is true )  {
    do this stuff here
}

If the condition has been specified in the parentheses ( ) is true or valid, the line will do whatever comes next inside of the curly brackets { }.

If the condition not meets and return false, then the stuff in the curly brackets will be ignored.

Sample 2.
We can also specify that our ‘stuff’ should only happen if a condition is NOT true. The way we do this is by adding a “!” to the start of our condition, like this

if ( !some condition is false )  {
    do this stuff here
}

Here ‘!‘ saying that left the ‘stuff’ which is with me and ‘do the stuff’ for others which is under the curly brackets{ }.

Make sure that ‘!‘ is without any space, otherwise it will work as the Sample 1.

Sample 3.
Another common conditional form to use is ELSE. First, a condition is checked and ‘stuff’ is done if the condition is true, but if the condition is false, then other ‘stuff’ is done.

if ( some condition is true )  {
    do this stuff here
} else {
    do this stuff here
}

Sample 4.
This sample is useful when you want to do multiple stuff using a single snippet.
We can use the conditional tag addition with ‘ELSEIF’ and ‘ELSE’.

if ( some condition is true )  {
    do this stuff here work 1
} elseif ( some 2nd condition is true ){
    do this stuff here work 2
}
else {
    do this stuff here work 3
}

Here, 1st a condition is checked and if returns true it will ‘do the stuff work 1’ otherwise it will check for the 2nd condition and if this return true, it will do the 2nd work and if this return false too, then it will do the stuff of work 3.

Using a Real Template Tag

There is a basic and common example to display some content on a specific page. Here we will use a conditional tag called is_page(). It will look like this

if( is_page( 'PAGE IDENTIFIER' ) ) {
   //do this 'stuff' here
}

Here, The PAGE IDENTIFIER is just a placeholder. This should be replaced with one of four things:

  • The title of the page i.e About and Contact  → is_page( ‘about’, ‘contact’ )
  • The ID number of the page i.e 3 and 4  → is_page( ‘5’, ‘6’ )
  • The unique slug of the page i.e /about/ and /contact/  → is_page( ‘about-me’, ‘contact-us’ )
  • A mixed array of page titles, IDs, and slugs i.e ‘About’, ‘5’, ‘contact’ →  is_page( array( 5, ‘about’, ‘contact’ ) );

Here are the possible entries which will return a true value.

  • is_page( ” )
  • is_page( 0 )
  • is_page( ‘0’ )
  • is_page( null )
  • is_page( false )
  • is_page( array() )

Let’s assume we are using the Page title for now. Here our page title is “About”, so what we could use:

Example 1.
This works when a user is on ‘About’ page.

if ( is_page( 'About' ) ) {
   echo 'This content is only shown on the About page'; 
}

Example 2.
This works when a user is not on ‘About’ page but is on other pages.

if ( !is_page( 'About' ) ) {
   echo 'This content is Not for About page but for others'; 
}

Example 3.

if ( is_page( 'About' ) ) {
   echo 'This content is only shown on the About page'; 
} elseif( is_page( 'Contact' ) ) {
   echo 'This is Contact-only page content'; 
}

Here how this snippet will work.

  1. if the user is on ‘About’ page = Do/Show the About page content ‘or’
  2. if the user is on Contact page = Do/Shows the Contact page content ‘or’
  3. if the user is on other pages = Don’t do anything/No content will be shown

Example 4.

if ( is_page( 'About' ) ) {
   echo 'This content is only shown on the About page'; 
} elseif( is_page( 'Contact' ) ) {
   echo 'This is Contact-only page content'; 
}
else {
  echo 'This is the other content and for all others';
}

Here how this snippet will work.

  1. if the user is on ‘About’ page = Do/Show the About page content ‘or’
  2. if the user is on Contact page = Do/Shows the Contact page content ‘or’
  3. if the user is on other pages = Do/Show content for Others

The examples are followed by the samples mentioned above.

Exploring Common WordPress Template Tags

There are dozens and dozens of conditional tags included with WordPress and available at Codex Page.

Here we are going to learn about most common conditional tags.

The Two most important, common and confused tags are 1. is_home() and 2. is_front_page()

Here, These two conditional tags work on the Reading settings. Go to SETTINGS >> Reading.

There are commonly 3 different options you can set in Reading.

  1. a default homepage (with the latest posts)
  2. a static homepage and no blog page
  3. a static homepage and a blog page

The Visual Conditions, When Your homepage displays

    1. Your latest posts
    2. A static page having

a) Homepage: A Static Page
b) Post Page: None (-Select-)

3. A static page having

a) Homepage: A Static Page (Frontpage)
b) Post Page: 2nd Page(Blog/post page)

1. is_home()

The 1st condition shows the latest posts and so is_home will return true.
The 2nd Condition shows A static page and so is_home will return false.
The 3rd Condition shows.
a) Homepage: A Static Page will return false.
b) Post Page: 2nd Page(Blog/post page) will return true.

This is how it will look like.

if( is_home() ) {
    echo ' 1. Assuming this is as default Reading setting 2. I have set it as Post page';    
}

!is_home() will left the same and do/show on other single post/pages.

is_paged() tag enables additional check if the post page contains paging like example.com/page/2/ , example.com/page/3 like this. Adding ! before is_paged() >> !is_paged() will prevent the function to do on pagination pages.

2. is_front_page()

The is_front_page() conditional tag return true in all three 1st, 2nd and 3rd conditions excluding 3. b) Post Page: 2nd Page(Blog/post page) = is_home()

if( is_front_page() ) {
    echo 'No matter if the default Reading setting is Your latest post or set as Homepage: A static Page, I will DO / Show  ';    
}

!is_front_page() will left frontpage/Homepage (depend on reading setting) and do/show on other single post/pages.

Here we are taking the 4th example to use the conditional tags in all 3 conditions correctly and in the right order to avoid bugs and test it in every user configuration:

if ( is_front_page() && is_home() ) {
  // DO/SHOW for Default homepage
} elseif ( is_front_page() ) {
  // DO/SHOW for static homepage
} elseif ( is_home() ) {
  // DO/SHOW for blog page
} else {
  //DO/SHOW for everything else
}

and using  || we can do the stuff for two and more. It may look like this

is_home() || is_front_page()
is_front_page() || is_home()

and the following will restrict doing the stuff until following i.e. two and more conditions are met.

is_home() && is_front_page()
is_front_page() && is_home()

&&  identify that other needs to be present too for doing the STUFF while || enable the work for two or more conditions.

and now let’s move to other conditional tags.

3. is_single()

This is used to detect the single ‘post’ page. It will not display on a single page but display on each single post one is visiting.

if ( is_single() ) {
    echo 'This displays when viewing a single blog post';    
}

This tag can accept parameters for the post title, slug, ID or a mixed array of these all.

if ( is_single('Hello World') ) {
    echo 'This displays when viewing a single blog post';    
}

Multiple Parameters for Single Post.

if ( is_single( array(8, 'Hello World') ) ) {
    echo 'This displays when viewing the "Hello World" post and the post with an ID of 8';   
}

4. is_page()

We have already talked about it in examples still, revise it.

if ( is_page( array('permalink-slug', 'Contact') ) ) {
    echo 'This displays when viewing the "Contact" page and the page with a slug of "permalink-slug"';
}

5. is_singular()

This tag most popular. It is a sort of combination of is_single() and is_page(). It returns true anytime we are viewing a single blog post, a page, an attachment, or any custom post type.
Like:-

if ( is_singular() ) {
    echo 'This displays anytime we are viewing a single post or page or custom post';    
}

A custom post page

if( is_singular( 'book' ) ) {
    echo 'This displays when viewing a single book detail post or page';    
}

multiple post types

is_singular( array( 'post', 'book' ) )

Simple note:-

Default Post Type
True when viewing a regular post.
is_singular( ‘post’ );

Custom Post Types
When any of the following return true: is_single(), is_page() or is_attachment(). is_singular();

True when viewing a post of the Custom Post Type book.

is_singular( 'book' );

True when viewing a post of the Custom Post Type newspaper or book.

is_singular( array( 'newspaper', 'book' ) );

Additional> >

if ( is_singular( $post_types = 'post', 'book' ) {
echo 'This displays when viewing a default single post or single custom book post or page'; 
}

6. is_category()

Default

if ( is_category() ) {
    echo 'This displays when viewing any category archive page';
}

For a single category:

if ( is_category( 'book' ) ) {
    echo 'This displays when viewing the "book" category archive';
}

For multiple categories

if ( is_category( array( 'book', 'pen' ) ) ) {
    echo 'This displays when viewing the "book" category and the "pen" category.

7. is_archive ()

if ( is_archive() ) {
    echo 'This displays when viewing Archive page of a Category';
}

Multiple Archive pages including custom archive page

if ( is_archive( array( 'post', 'portfolio' ) ) ) {
    echo 'This displays when viewing Archive page of a default post or custom post 'portfolio' archive';
}

8. has_tag()

if ( has_tag( 'Featured' ) ) {
    echo 'This displays when a post has the featured tag';
}

We can also check for the existence of multiple tags by using an array:

if ( has_tag( array( 'Featured', 'Updates' ) ) ) {
    echo 'This displays when a post has the featured or Updates tag';
}

9. is_search()

if( is_search() ) {
    echo 'I am doing my work because you are viewing search results';
}

Others useful tags are

  • is_post_type_archive for custom post type archive
  • is_tag() for Tag archive page
  • is_tax() for custom post tag archive page

Note:- The echo line is just for reference only. The line is generally working or acts as to do a specific work/action like to

  1. Add a content and display it
  2. Remove a content
  3. Register a content and display it (.css *style, .js *script, sidebar, widgets )
  4. Deregister a content (.css *style, .js *script, sidebar, widgets )
  5. Enqueue a content (.css *style .js *script )
  6. Dequeue a content (.css *style .js *script )

Using Conditional Tags in Genesis

What we have to do is to create a function and add it using a Genesis hook but I love to follow the steps like this
1. Add an action using hooks and write a function name

add_action( 'genesis_before_content', 'simple_conditional_snippet' );

2. Create the function using the function name created in 1st step.

function simple_conditional_snippet() {
  echo 'This is Conditional Snippet for all';
  }

3. Add Conditional Tag

Here is how the Conditional Tag snippet will look like

//* Simple Conditional Snippet
add_action( 'genesis_before_content', 'simple_conditional_snippet' );
function simple_conditional_snippet() {
if ( is_singular('post') ) { //Hey I am here
  echo 'This is Conditional Snippet for Single Post Page which will be shown genesis_before_content';
  }
}

Result

Another Example to Enqueue Stylesheet and Script

//* Register and Enqueue Stylesheet and Script on Front Page only
add_action( 'wp_enqueue_scripts', 'enqueue_conditional_frontpage_style_script' );
function enqueue_conditional_frontpage_style_script() {

if ( is_front_page() ) {
    wp_enqueue_style( 'front-page-style', get_stylesheet_directory_uri() . '/css/front-page.css', false, '2.0.0' );
    wp_enqueue_script( 'front-page-script', get_stylesheet_directory_uri() . '/js/front-page.js', array ( 'jquery' ), '2.0.0', true );
}

}

Show WP-Post ratings on Single Post only

add_action('genesis_after_entry_content', 'wp_post_ratings');

function wp_post_ratings() {
	if( is_singular('post') && function_exists('the_ratings')) { the_ratings(); }
}

Don’t forget to check the following resources and Genesis Hooks Guide.

============= Reference :-

Conditional Tags from Codex and Theme Handbook

Template and Plugin API and Post Types

Additional Resources

WooCommerce Conditional Tags and bbPress Conditional Tags

Related Posts

  • Conditional Tag is_front_page() v/s is_home(). What to use?
  • Add Custom Body Class in Genesis
  • Conditional site footer in Genesis
  • Replace site footer widget area conditionally in Genesis
  • Customize Post Meta conditionally in Genesis

Categories: Free Content, WordPress Tutorials Tags: conditional tags, conditionally, Tags

Reader Interactions

Primary Sidebar

Search

WPEngine WordPress Hosting, Perfected.

Hosting You are looking for?.
Perfect solution for small business to global enterprise.

Learn more

StudioPress Genesis Theme Framework

The No.1 Theme Provider.
Creative, SEO rich Theme for all niche projects.

Learn more

Categories

  • Free Content
  • Genesis Tutorials
  • Premium Content
  • Snippets
  • What's New?
  • WordPress Tutorials

Tag Cloud

Archive Background Section blog canvas menu center logo columns conditional tags CSS CSS Grid custom Customizer custom post type Custom Post Types custom template Custom Widget effect eNews Extended Featured Image front-page Genesis Genesis Sample header right hero section Image Background js layout left menu Logo menu Navigation Menu newsletter post page related posts responsive menu search search widget Shrinking Logo site header slide in-out Stylesheet Template Utility Bar Video Background widgets WordPress

Built with Genesis Framework + WordPress by Aryan Raj

  • Contact
  • FAQ
  • Disclaimer
  • Privacy Policy
  • Copyright Policy
  • Terms of Service