$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Echo Text If User Is Logged in But NOT the Author of post|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin11PV0评论

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; ?>
Post a comment

comment list (0)

  1. No comments so far