0

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,

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.

1
  • 1
    Thank you very much for your help. I really appreciate it. That worked like a charm. Jun 19 '20 at 23:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.