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
Add a comment  | 

1 Answer 1

Reset to default 0

The 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