• 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 and Return Early in Genesis

Last Updated on August 21, 2019 Favorited: 0 times

This tutorial is still in progress…

This is an article in the reference of the tutorial conditional tags in WordPress and Genesis.

Make sure to give a read to Conditional Tags in WordPress and Genesis to have a better understanding of return early Code tip.

While that article is a good fit for newbies, it is recommended to follow this article while writing any code. This ensures that we have a clean and well-structured code that is easy to understand and works faster.

What are Conditional Tags?

Conditional tags are the rule that provides the facility to control ‘Doing the stuff for different contents’.

Here, Doing the STUFF refers to…
1. Adding or removing a filter
2. Adding or removing an action
more…
and not limited to.

Different Contents could be

1. Posts or Pages includes Frontpage, Homepage aka post page and Custom Post Types
2. Archive, Categories includes Author archive and Custom taxonomy.
3. By user role i.e Logged, Unregistered, Admin
and more
and not limited to.

Not limited to means there are more STUFF and CONTENTS present in WordPress and Genesis that can be done using conditional tags.

You can read more about Conditional Tags here.

Return Early

Return early simplify the work for the processor for doing the work which helps to quickly act for the specific command.

REMEMBER => Return early works opposite of what we do in the old way. While we are restricting the condition using ! in old way the new way use it to show on the condition which it is attached to.

OLD WAY
is_front_page() => The STUFF will be done on front page only.
!is_front_page() => The STUFF will not done on Front page but will on OTHER pages.
! is_front_page() => The STUFF will done for Front page only.

1st and 3rd work similarly.

RECOMMENDED WAY
when we use Return early.

is_front_page() => The STUFF will be NOT done on front page.
!is_front_page() => The STUFF will ONLY done on Front page and NOT on OTHER pages.
! is_front_page() => The STUFF will ONLY done on Front page and NOT on OTHER pages.

2nd and 3rd work similarly.

Sample:-

Let’s what we do in the OLD WAY

1.

if ( is_front_page() ) {
echo 'You are viewing Front Page';
}

2.

if ( !is_front_page() ) {
echo 'You are NOT on Front Page';
}

3.

if ( ! is_front_page() ) {
echo 'You are viewing Front Page';
}

 

RECOMMENDED WAY

1.

if ( is_front_page() ) {
return;
}
echo 'You are NOT on Front Page';

2.

if ( !is_front_page() ) {
return;
}
echo 'You are viewing Front Page';

3.

if ( ! is_front_page() ) {
return;
}
echo 'You are viewing Front Page';

Example:-

Let’s how we can use it in Genesis site using WordPress hooks and Genesis hooks location with a custom function.

OLD WAY

Add only one of the example from the following in child-theme/functions.php

//* Example 1.
add_action( 'genesis_before_content', 'old_conditional_snippet' );
function old_conditional_snippet() {
if ( is_front_page() ) { //Hey I am here
echo 'Some STUFF to do for front page using OLD WAY';
}
}

//* Example 2.
add_action( 'genesis_before_content', 'old_conditional_snippet' );
function old_conditional_snippet() {
if ( !is_front_page() ) { //Hey I am here
echo 'Some STUFF to do NOT for front page using OLD WAY';
}
}

//* Example 3.
add_action( 'genesis_before_content', 'old_conditional_snippet' );
function old_conditional_snippet() {
if ( ! is_front_page() ) { //Hey I am here
echo 'Some STUFF for front page using OLD WAY';
}
}

RECOMMENDED WAY

Add only one of the example from the following in child-theme/functions.php

//* Example 1
add_action( 'genesis_before_content', 'new_conditional_snippet' );
function new_conditional_snippet() {
if ( is_front_page() ) { //Hey I am here
return;
}
echo 'Some STUFF to do NOT for Front Page using RECOMMENDED WAY';
}

//* Example 2
add_action( 'genesis_before_content', 'new_conditional_snippet' );
function new_conditional_snippet() {
if ( ! is_front_page() ) { //Hey I am here
return;
}
echo 'Some STUFF to do for Front Page using RECOMMENDED WAY';
}

//* Example 3
add_action( 'genesis_before_content', 'new_conditional_snippet' );
function new_conditional_snippet() {
if ( ! is_front_page() ) { //Hey I am here
return;
}
echo 'Some STUFF to do for Front Page using RECOMMENDED WAY';
}

Multi Conditions and return early works same opposite of the old way.

Generally what we have used in old way.
is_home() || is_paged() => work on example.com and example.com/page/2*
is_home() && is_paged() => work on example.com/page/2*
is_home() || !is_paged() => work on example.com only.

Here, && = Additional condition to be met.
|| = work on other condition too.

The new way.
!is_home() || !is_paged() => work on example.com and example.com/page/2*
!is_home() && is_paged() => work on example.com.
is_home() || !is_paged() => work on example.com/page/2*

Here, && = work on other condition too.
|| = Additional condition to be met.

Remember:- However, this will Totally depend on using ! with the conditions.

Real-life Examples:

You can found one here in Show WP-PostRatings Plugin Ratings in Genesis

Example 1. When you not want to to the stuff on front page and post page using return early function.

if ( is_front_page() || is_home() ) { //here || = && in old way with ! required.
return;
}

Example 2. When I don’t want to show facetwp dropdown on paging what i have used.

if ( ! is_home() || is_paged() ) {//* Don't show on /page/2*
return;
}

find here. Facetwp Dropdown in Genesis.

Example 3. When I have to show the widgets only if the sidebar is active or not.

if (! ( is_active_sidebar('utility-bar-left') || is_active_sidebar('utility-bar-right') ) ) {
return;
}

Find here. Utility Bar in Genesis and Utility bar in Genesis Sample.

Widget Example.

Show widget on the front page only and check Whether a sidebar is in use. If not, return early.

    if (! is_front_page() || ! is_active_sidebar('hero-section') ) {
        return;
    }

or can be also done using this.

    if ( ! is_front_page() && is_active_sidebar('hero-section') ) {
        return;
    }

This will work too.

if ( ! ( is_front_page() && is_active_sidebar('hero-section') ) ) {
        return;
    }

Show Globally ( to do stuff for other pages too.)

     if ( ! ( is_front_page() || is_active_sidebar('hero-section') ) ) {
        return;
    }

What have you learned?

When done, return. Otherwise, continue processing.

Your turn.

Go, launch a test site, Grab the old codes from old conditional snippets and make it work using the return early function and share your result with me.

Sometimes, it seems to be confusing but keeps practicing it. This post will update soon with more examples and explanations.

Related Posts

  • Post Page Blog Archive Template with Layouts in Genesis
  • 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

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

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