【问题标题】:Hide a CSS class for a specific page BUT not for the URIs / paths of that page隐藏特定页面的 CSS 类,但不隐藏该页面的 URI / 路径
【发布时间】:2023-03-12 13:40:02
【问题描述】:

背景信息:

我有一个 Wordpress 插件来显示每个用户个人资料(前端)。当登录的用户访问他们的个人资料时。网址是 /profile/。当用户访问另一个配置文件时,路径相同,后跟用户名 /profile/bob1/(例如)。

使用一些 div 类,我创建了一个模糊的背景,顶部有另一个图像,以阻止某些 Wordpress 用户查看个人资料信息。

问题:

我想知道如何在他们自己的个人资料页面上隐藏这些 div 类不是:/profile/ 但它的所有其他路径 /profile/bob1/ (例如)以及其他'if' 条件你可以在下面看到:

(如您所见,我设法只向具有 Wordpress '阅读'能力的特定用户显示 div 类......)

    <div class="back"<?php if (current_user_can('read')){ echo 'style="display:none;"'; } ?>></div>

非常感谢您看这个!非常感谢。

【问题讨论】:

  • 只是为了明确我需要将“current_user_can”保留在那里。谢谢。
  • 如果您使用的是 PHP,为什么还要包含配置文件信息?当然,敏锐的用户所要做的就是将网页保存在本地并更改样式声明。

标签: php wordpress class hide uri


【解决方案1】:

来自Author Templates 的 WordPress 文档。

<?php 
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
?>

所以应用相同的逻辑。

<?php 
$viewother = (isset($_GET['author_name'])) ? true : false;
?>

或者您可以检查用户 ID。

<?php
$style = '';
$curauth = get_userdata(intval($author));
$otherauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : false;
if($otherauth && $otherauth->ID != $curauth->ID) {
    // user is viewing another profile
    $style = ' style="display:none;"';
}
?>

<div class="back"<?php echo $style; } ?>></div>

【讨论】:

  • 非常感谢@slapyo。我是 PHP 的新手。请您展示您的代码如何与我上面编写的代码相结合。非常感谢。
猜你喜欢
  • 2014-04-30
  • 2016-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多