Conditional Genesis Post Info

This tutorial provides 2 different ways to modify what entry meta is displayed in the post info.

If you want to modify the post info conditionally, you can use a :

  1. if…else statement or
  2. a ternary operator.

if…else statement #

These code snippets will display the post author along with the other standard entry meta when the post is in category news.

If not in category news, the post author is removed and only the date, comments and edit link are displayed.

Add 1 of the following code snippets only to your child themes functions file.

add_filter( 'genesis_post_info', 'post_info_filter' );
function post_info_filter( $post_info ) {

if ( in_category('news') ) {

$post_info = '[[post_author]] [[post_date]] [[post_comments]] [[post_edit]]';

} else {

$post_info = '[[post_date]] [[post_comments]] [[post_edit]]';

    }
    
return $post_info;
    
}

You can also write the code like this :

add_filter( 'genesis_post_info', 'post_info_filter' );
function post_info_filter( $post_info ) {

if ( in_category('news') ) :

$post_info = '[[post_author]] [[post_date]] [[post_comments]] [[post_edit]]';

else :

$post_info = '[[post_date]] [[post_comments]] [[post_edit]]';

endif;
    
return $post_info;
    
}

Learn more about using if else and if elseif else conditional statements.

ternary operator #

You can also use a ternary operator with conditional tag and write the filter function like this :

add_filter( 'genesis_post_info', 'post_info_filter' );
function post_info_filter( $post_info ) {

$post_info = in_category('news') ? '[[post_author]] [[post_date]] [[post_comments]] [[post_edit]]' : '[[post_date]] [[post_comments]] [[post_edit]]';

return $post_info;
    
}

Here’s another way to write the code using variables :

add_filter( 'genesis_post_info', 'post_info_filter' );
function post_info_filter( $post_info ) {

$include_author = '[[post_author]] [[post_date]] [[post_comments]] [[post_edit]]';

$remove_author = '[[post_date]] [[post_comments]] [[post_edit]]';

$post_info = in_category('news') ? $include_author : $remove_author;

return $post_info;
    
}

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.