Simplifying how I currently show featured posts, when a post is submitted by a full member, a post_meta value is added to the post which is then styled differently.
But Id like to do it based on the post authors, user role instead.
So if a the user who submitted the post is a “full_member”, their post is automatically featured without the need for post_meta to be queried.
I’ve tried something like this which is in the archive template loop, and just adds a “featured-listing” class to a wrapping div, but I don’t think I’m looking at it in the correct way.
<?php $user = wp_get_current_user();
if ( in_array( 'full_member', (array) $user->roles ) ) { ?>
featured-listing
<?php } ?>
EDIT
Part 1 has been resolved using Nathan’s original answer for the class issue , his answer has expanded since then which doesnt work for me, so ive pasted the solution that did work below :
//* If the post author is a full member, they get a featured listing
function which_class() {
return post_author_role_in( 'full_member' ) ? 'featured-listing' : 'regular-listing';
}
//* Determine if the post author is in the $role
function post_author_role_in( $role ) {
return in_the_loop() ? user_role_in( get_the_author_meta( 'ID' ), $role ) : false;
}
//* Determine if the $user_id is in the $role
function user_role_in( $user_id, $role ) {
return in_array( $role, ( new WP_User( $user_id ) )->roles );
}
Part 2 However I still need a solution for
Id still like to use a conditional to completely hide certain elements within the template, is there an IF condition which says something like IF post author is in X role, display this, else display this
. ?
Please advise 🙂