简体   繁体   English

如果当前用户是帖子的作者,则在前端显示图标

[英]Show icon on frontend if current user is the author of post

When a user registers on my website, a single post is automatically created (under a custom post type) with the user assigned as the author to that post.当用户在我的网站上注册时,系统会自动创建一个帖子(在自定义帖子类型下),该用户被指定为该帖子的作者。 This post is essentially the user's "profile page".这篇文章本质上是用户的“个人资料页面”。

Any user is able to see any other users profile page.任何用户都可以看到任何其他用户的个人资料页面。 However, I would like a button to appear on the profile page if it belongs to the logged in user.但是,如果它属于登录用户,我希望在个人资料页面上显示一个按钮。

In other words: if the logged-in user is the author of the post that is currently being viewed (on the frontend), then display the button.换句话说:如果登录用户是当前正在查看的帖子的作者(在前端),则显示按钮。

The below is the closest I have come to achieving what I need to do - I have created a button with css id #profile_edit_button and hidden it by default using display:none .下面是我最接近实现我需要做的事情 - 我创建了一个带有 css id #profile_edit_button的按钮,并默认使用display:none隐藏它。 The code below "unhides" the button, but on all profile pages.下面的代码“取消隐藏”按钮,但在所有个人资料页面上。 It also throws out the following errors:它还会抛出以下错误:

Warning: Undefined variable $user_session_id Warning: Undefined variable $author_id警告:未定义变量 $user_session_id 警告:未定义变量 $author_id

The code I've used is as follows.我使用的代码如下。 Any guidance would be greatly appreciated.任何指导将不胜感激。

 if($user_session_id == $author_id) {
    ?>
    <style type="text/css">
        #profile_edit_button {
            display: flex !important;
        }
        </style>;
    <?php } ?>

Try the below code.试试下面的代码。 code will go in your active theme functions.php file.代码将进入您的活动主题functions.php 文件。

function current_user_is_post_author(){
    if( is_single() ){
        global $post;
        if( get_current_user_id() == $post->post_author ) { ?>
            <style type="text/css">
                #profile_edit_button   {
                    display: flex !important;
                }
            </style>;
    <?php } }
}
add_action( 'wp_head', 'current_user_is_post_author', 10, 1 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM