functions - Hide author info in single posts by certain users

I use this code to add custom CSS if single post and in certain categories:function hide_author() {global $post;if (is_

I use this code to add custom CSS if single post and in certain categories:

function hide_author() {
    global $post;
    if (is_single($post->ID) && in_category(array( 'category 1', 'Category 2'), $post->ID)) {
            ?>
    <style type="text/css">
.author-info {display: none;}
    </style> 
    <?php
    }
}
add_action('wp_head', 'hide_author');

I was wondering how to fire wp_head action if single post by certain users (either by user ID or username)? Can I use wp_set_current_user ()? if so, how to use it exactly?

Thanks,

I use this code to add custom CSS if single post and in certain categories:

function hide_author() {
    global $post;
    if (is_single($post->ID) && in_category(array( 'category 1', 'Category 2'), $post->ID)) {
            ?>
    <style type="text/css">
.author-info {display: none;}
    </style> 
    <?php
    }
}
add_action('wp_head', 'hide_author');

I was wondering how to fire wp_head action if single post by certain users (either by user ID or username)? Can I use wp_set_current_user ()? if so, how to use it exactly?

Thanks,

Share Improve this question asked Jun 19, 2020 at 2:20 Ismail ElfaIsmail Elfa 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

No, there's no need to use wp_set_current_user().

I was wondering how to fire wp_head action if single post by certain users (either by user ID or username)?

You can use $post->post_author to get the ID of the author of the post, and put the user ID list in an array, then just do in_array() to check if the author ID is in the ID list. For example:

function hide_author() {
    //global $post;     // Instead of this,
    $post = get_post(); // I would use this one.

    // Define the user ID list.
    $user_ids = array( 1, 2, 3 );

    if (
        $post && is_single( $post->ID ) &&
        in_array( $post->post_author, $user_ids )
    ) {
    ?>
        Your code here.
    <?php
    }
}

And that example would run only on single post pages and that the current post's author ID is in the $user_ids list/array.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742334390a4424359.html

相关推荐

  • functions - Hide author info in single posts by certain users

    I use this code to add custom CSS if single post and in certain categories:function hide_author() {global $post;if (is_

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信