我需要一个按钮出现时,没有登录和另一个登录时,我需要帮助修复代码请!

时间:2016-11-07 作者:G.Brown

我想在用户注销/登录时这样做(&N);查看其他用户配置文件“仅查看联系人卖家”按钮(&A);当用户登录查看自己的个人资料时,会显示“查看收件箱”&;编辑我的个人资料按钮。这是一个屏幕,显示以管理员身份登录的用户查看属于flamez的配置文件,并看到错误的按钮https://www.dropbox.com/s/lifcq06y54jol7z/Screenshot%20of%20error.png?dl=0

以下是我目前掌握的代码:

<style>
a.view_inbox_btn:link, a.view_inbox_btn:visited{
    background: Green;
    display: inline-block;
    padding: 9px 0px 9px 0px;
    color: #fff;
    text-align: center;
    text-decoration: none;
    border: 1px solid #457D2B;
    width: 25%;
    font-size: 23px;
    font-weight: bold;
    font-family: \'Alegreya Sans\',Arial, Helvetica, sans-serif;
    border-radius: 3px;
    margin-left: 5px;
    margin-top: 52px;}
a.view_inbox_btn:hover, a.view_inbox_btn:active {
    background-color: #4da64d;
}
a.edit_profile_btn:link, a.edit_profile_btn:visited{
    background: #ff7f00;
    display: inline-block;
    padding: 9px 0px 9px 0px;
    color: #fff;
    text-align: center;
    text-decoration: none;
    border: 1px solid #ff7f00;
    width: 25%;
    font-size: 23px;
    font-weight: bold;
    font-family: \'Alegreya Sans\',Arial, Helvetica, sans-serif;
    border-radius: 3px;
    margin-left: 11px;
    margin-top: 17px;}
a.edit_profile_btn:hover, a.edit_profile_btn:active {
    background-color: orange;
}
a.contact_seller_btn:link, a.contact_seller_btn:visited{
    background: Green;
    display: inline-block;
    padding: 9px 0px 9px 0px;
    color: #fff;
    text-align: center;
    text-decoration: none;
    border: 1px solid #457D2B;
    width: 25%;
    font-size: 23px;
    font-weight: bold;
    font-family: \'Alegreya Sans\',Arial, Helvetica, sans-serif;
    border-radius: 3px;
    margin-left: 5px;
    margin-top: 52px;}
a.contact_seller_btn:hover, a.contact_seller_btn:active {
    background-color: #4da64d;
}
</style>    

<?php if (is_user_logged_in() ): ?>
<a href="http://enormu.com/marketplace/my-account/private-messages/"class="view_inbox_btn">View Inbox</a>
<a href="http://enormu.com/marketplace/my-account/personal-information/"class="edit_profile_btn">Edit Profile</a>
<?php else: ?> 
<a href="http://enormu.com/marketplace/my-account/private-messages/"class="contact_seller_btn">Contact Seller</a>
      <?php endif ?>
   </ul>
</div>

2 个回复
SO网友:appartisan

我认为实现的最简单的方法之一(也许不是更好)是创建一个类“btn hidden”或类似的,带有display:定义中没有。然后,当您在模板中时,控制用户是否登录(函数is\\u user\\u logged\\u in()),以及用户是否在一个或另一个页面中(根据您的配置,这可以通过不同的方式完成,可能可以通过is\\u page(“page Name”)函数)。最后,根据这些元素的组合,将该类应用于应该隐藏的按钮。

希望这能有所帮助,但我认为这个问题需要更多关于背景的信息。

SO网友:appartisan

如果页面作者是需要比较的用户,则可以使用get\\u the\\u author\\u meta(\'id\')。如果没有,也许可以从url获取尼克,并从其id在数据库中搜索。

$currentUserId = get_current_user_id();
$lastPartOfUrl = \'flamez\'; // Get from the url, as in your screenshot
$table = $wpdb->prefix.\'users\';
$page_owner_id = $wpdb->get_var("select ID from $table where user_login=\'$lastPartOfUrl\'");

//Then compare
if($currentUserId === $page_owner_id){
    //We show the edit profile button
}else{
    //We show the other button
}
希望这有助于更好地理解。注意清理$lastPartOfUrl变量,因为它会破坏您的代码或将您暴露于SQL injection.

[编辑]如果您不知道url的最后一部分,可以按如下方式提取:

$currentPage = $_SERVER[\'REQUEST_URI\']; 
$parts = explode(\'?\',$currentPage); 
$lastPartOfUrl = basename(trim($parts[0],\'/\'));

相关推荐

显示作者姓名PHP(自制插件)

我有一个需要帮助的问题,因为我自己找不到解决办法。我接管了一个网站,之前有人在那里创建了一个自制插件。。使用默认插件“Contact Form 7”,用户可以在页面上创建帖子。()https://gyazo.com/c8b20adecacd90fb9bfe72ad2138a980 )关于自行创建的插件“Contact Form 7 extender”,帖子是通过PHP代码在后台生成的(https://gyazo.com/115a6c7c9afafd2970b66fd421ca76a3)其工作原理如下:如果