php – Show sidebar only to author of post


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();