If post by specific role different css to everyone

So i have searched alot but cant find a solution.
Every solution which i find is changing css only to that specific role not for everyone else.

Basicly i want to do this:

If user role contributor then do this to his post and also so everyone can see it
.type-post { border: 10px solid #fff }
else
.type-post { border: 20px solid #000 }

So if a post i made by a specific user who has user role contributor his post borders color will be white and 10 px and everyone should see that not only people with that user role.
If post is made by everyone one else it should have default css or a css of my pick and everyone should see that including people with user role contributor.

Hope someone can help me out!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can update the post classes depending on user role to include an additional selector which add your custom styling i.e

function add_contributor_class_to_single_post( $classes ) { 

    if ( is_single() ) {
        $post_id = get_queried_object_id();
        $author_ID = get_post_field( 'post_author', $post_id );
        $author_data = get_userdata( $author_ID );
        if (in_array( 'contributor', $author_data->roles)) {
            // add post class
            array_push( $classes, 'user-contributor' );
        }
    }
    return $classes;    

}   
add_filter( 'post_class', 'add_contributor_class_to_single_post' );

Then update your css to:

.type-post.user-contributor { border: 10px solid #fff }
.type-post { border: 20px solid #000 }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x