最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

If post by specific role different css to everyone

programmeradmin1浏览0评论

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!

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!

Share Improve this question edited Mar 8, 2021 at 22:35 ANdy asked Mar 8, 2021 at 22:29 ANdyANdy 478 bronze badges 1
  • this can't be done with just CSS, the role of the author isn't present in the HTML markup by default, PHP code is needed – Tom J Nowell Commented Mar 8, 2021 at 22:52
Add a comment  | 

1 Answer 1

Reset to default 3

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 }
发布评论

评论列表(0)

  1. 暂无评论