• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2665

Original Genesis Tutorials & 5000+ Guaranteed Code

Snippets

  • Support
  • Newsletter
  • Videos
  • Log in

Premium Member? - Request custom code

if else

if …. else is used as a conditional statement or conditional expression which enables you to execute code if a specific condition returns true otherwise execute other code if the same condition is false.

Example :

if ( is_front_page() ) {
    echo 'This is your front page';
} else {
    echo 'This text prints on all pages excluding your front page';
}

You can test the above code by adding it to your child themes functions file however you’ll first need to wrap it in a action hook like this :

add_action( 'genesis_after_header', 'if_else_code_test' );

function if_else_code_test() {

    if ( is_front_page() ) {
        echo 'This is your front page';
    } else {
        echo 'This text prints on all pages excluding your front page';
    }

}

You can also use conditional tags with a ternary operator to achieve the same result.

if…elseif….else #

if …. elseif …. else enables you to execute code if specific conditions return true otherwise execute code if the conditions return false. Watch video.

Example :

if ( is_front_page() ) {
    echo 'This prints on your front page';
} elseif ( is_singular('post') ) {
    echo 'This text prints on all single posts excluding your front page';
} else {
    echo 'Print this on all pages if all other conditions return false.
}

To test, modify and use this code, you can paste this code at the end of your child themes functions file, wrapped in any hook like this.

add_action( 'genesis_after_header', 'if_elseif_else_code_test' );

function if_elseif_else_code_test() {

   if ( is_front_page() ) {
        echo 'This prints on your front page';
    } elseif ( is_singular('post') ) {
        echo 'This text prints on all single posts excluding your front page';
    } else {
        echo 'Print this on all pages if all other conditions return false';
    }

}

The above code prints unique text on the front page, unique text on all single posts and unique text on all other page/post types.

Watch Video #

The following video uses the above code snippet ( with added styling & different hook ) to demonstrate how you can use if elseif else conditional statements with conditional tags, in any Genesis child theme.

Related Tutorials

  • What Is a Ternary Operator
  • Conditional Genesis Post Info

PHP Functions

Reader Interactions

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

PHP Code

template_include

get_body_class

if else

array

class_exists

foreach

sprintf

add_action

printf

variable

Advertise · WPEngine · Genesis · Log in

  • How Premium Membership Works
  • Sign Up
  • Support
  • Subscription Details/Invoice
  • Tagged Tutorials
  • Access-Download Problems