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,

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
scroll top