如何限制来自 Wordpress 中具有特定角色的用户的内容? [英] How to restrict content from Users with Specific roles in Wordpress?

查看:33
本文介绍了如何限制来自 Wordpress 中具有特定角色的用户的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想隐藏登录用户(订阅者)的广告.我正在使用一个名为private"的插件来生成短代码.我找到了我应该使用的代码,但不知道如何将它放在我的 single.php 中.我的单个帖子是定制的,所以我不能使用富编辑器.

i want to hide ads for logged in user (subscribers). im using a plugin called "private" that generates shortcodes. i found the code i should use but dont know how to put it in my single.php. My single post is custom made so i cant use the rich editor.

我想用的那个...

[private role="subscriber"]内容在这里[/private]

我想把这段代码放在我的短代码的中间位置.

and i want to put this code in the middle position of my short code.

<div id="M589567ScriptRootC925056"></div>
<script src="https://jsc.adskeeper.co.uk/a/n/anime-tv.fun.925056.js" async></script>

推荐答案

您不必使用 [private] 短代码来隐藏 divs 或您的案例中的广告来自登录用户.

You do not have to use [private] short codes to hide divs or ads in your case from logged in users.

只需使用 WordPress 原生 current_user_can() 函数来定位 PHP 文件中这样的特定用户角色

Just use WordPress native current_user_can() function to target specific user roles like this in PHP file

if (current_user_can('subscriber')) {

   //Hide something

} else {

   //Show Something

}

如果你想对所有登录的用户隐藏一些东西,你可以做一些事情

You can do something if you want to hide something from all logged in users

if (is_user_logged_in) {

   //Hide something

} else {

   //Show Something

}

此代码进入您的工作 php 文件或活动主题 function.php

This code goes into your working php file or active theme function.php

这篇关于如何限制来自 Wordpress 中具有特定角色的用户的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆