admin管理员组文章数量:1418669
I'm trying to find a way to add a body class "author" IF the current user is the author of the post they are viewing.
This is what I have so far...
add_filter( 'body_class','my_body_classes' );
function my_body_classes( $classes ) {
if ( !$current_user->ID == $post->post_author ) {
$classes[] = 'post-author';
}
return $classes;
}
I'm trying to find a way to add a body class "author" IF the current user is the author of the post they are viewing.
This is what I have so far...
add_filter( 'body_class','my_body_classes' );
function my_body_classes( $classes ) {
if ( !$current_user->ID == $post->post_author ) {
$classes[] = 'post-author';
}
return $classes;
}
Share
Improve this question
asked Jul 27, 2019 at 6:58
PetePete
1,0582 gold badges14 silver badges40 bronze badges
1
|
1 Answer
Reset to default 1The code isn't working because you haven't defined or retrieved the $current_user
or $post
variables from anyway. You've also got a !
here for some reason: !$current_user->ID
, which will just break the condition.
You need to use the appropriate functions to get their values, and also use is_single()
to make sure you're viewing a single post (otherwise the post author could be missing or something unexpected).
add_filter(
'body_class',
function( $classes ) {
if ( is_single() ) {
$post = get_queried_object();
$user = wp_get_current_user();
if ( $user->ID == $post->post_author ) {
$classes[] = 'post-author';
}
}
return $classes;
}
);
本文标签: post body class for current user only if they are the post author
版权声明:本文标题:post body class for current user only if they are the post author 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745292232a2651865.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
if
expression. Comparison Operators – Max Yudin Commented Jul 27, 2019 at 7:31