admin管理员组文章数量:1323348
I am trying to show the sidebar only if the user viewing the post is the author of that post. So anyone else viewing the post, if they are not the author, then they cannot see the sidebar.
So if "John" wrote 'Post A' but he did not write 'Post B', then "John" could see the sidebar when he is viewing Post A, but he could not see it when viewing 'Post B'.
I have tried many variations of this but nothing seems to do the trick. Either the sidebar disappears for everyone, including the author, or it is visible to everyone.
Also, I am using a custom post type that I made called 'campaign', not sure if that is why this isn't working.
$author_id = get_post_field ('post_author', $post_id);
$current_user = wp_get_current_user();
function showSidebarWhenNeeded()
{
if ( $current_user->ID !== $author_id ) {
echo '<style>.widget-area{ display: none !important;}</style>';
}
}
showSidebarWhenNeeded();
I am trying to show the sidebar only if the user viewing the post is the author of that post. So anyone else viewing the post, if they are not the author, then they cannot see the sidebar.
So if "John" wrote 'Post A' but he did not write 'Post B', then "John" could see the sidebar when he is viewing Post A, but he could not see it when viewing 'Post B'.
I have tried many variations of this but nothing seems to do the trick. Either the sidebar disappears for everyone, including the author, or it is visible to everyone.
Also, I am using a custom post type that I made called 'campaign', not sure if that is why this isn't working.
$author_id = get_post_field ('post_author', $post_id);
$current_user = wp_get_current_user();
function showSidebarWhenNeeded()
{
if ( $current_user->ID !== $author_id ) {
echo '<style>.widget-area{ display: none !important;}</style>';
}
}
showSidebarWhenNeeded();
Share
Improve this question
asked Sep 6, 2020 at 22:36
orange-ocorange-oc
11 bronze badge
1 Answer
Reset to default 0The reason this is not working is because you are trying to use undefined variables, they are declared outside the function and can therefor not be used without prefixing them with the global
keyword.
If you have error_reporting
enabled you would see warnings like this:
Notice: Undefined variable: current_user in /... on line 11
Notice: Undefined variable: author_id in /... on line 11
You enable debugging by setting WP_DEBUG
to true
in your wp-config.php
file.
Adding the global
keyword would make the variables accessible from within the function:
function showSidebarWhenNeeded()
{
global $current_user;
global $author_id;
if ( $current_user->ID !== $author_id ) {
// ...
}
}
I would instead set the variables inside the function:
function showSidebarWhenNeeded($post_id)
{
$author_id = get_post_field ( 'post_author', $post_id );
$current_user = wp_get_current_user();
if ( $current_user->ID !== $author_id ) {
// ...
}
}
本文标签: phpShow sidebar only to author of post
版权声明:本文标题:php - Show sidebar only to author of post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742130571a2422139.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论