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.
Was This Tutorial Helpful?