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 badge1 Answer
Reset to default 0No, 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
评论列表(0条)