Echo Text If User Is Logged in But NOT the Author of post

admin2025-06-03  4

i am coding a plugin, where user can subscribe to a indvidual author of post (like we do on YouTube). I want to display the subscribe link to all logged in users, but not the author of post.

my plugin gets the author id for each post and display's the link. Problem is author would not subscribe to his own self,

So in short i want a way to echo a link, if current user is NOT the author of the post.

i am coding a plugin, where user can subscribe to a indvidual author of post (like we do on YouTube). I want to display the subscribe link to all logged in users, but not the author of post.

my plugin gets the author id for each post and display's the link. Problem is author would not subscribe to his own self,

So in short i want a way to echo a link, if current user is NOT the author of the post.

Share Improve this question asked Feb 3, 2019 at 6:18 user160406user160406
Add a comment  | 

1 Answer 1

Reset to default 1

Try something like this in your template tags (or wherever in your theme):


function should_display_link() {
    $post = get_post();

    // Something went wrong, bail. (You should always have a post, in theory)
    if ( ! $post ) {
        return false;
    }

    $current_user = get_current_user_id();

    // Assuming only logged-in users can subscribe.
    if ( ! $current_user ) {
        return false;
    }

    return absint( $post->post_author ) !== absint( $current_user );
}

In your template, you can use it like this:

<?php if ( should_display_link() ) : ?>
    <!-- Your link code here -->
<?php endif; ?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748949563a315081.html

最新回复(0)