WordPress强化-两段代码实现文章内容登录可见内容

给自己的WordPress网站增加一个会员可读范围,会员需要登录后才能查看相关内容。

将下面的代码添加到主题functions.php模板文件中:

function member_check_shortcode($atts, $content = null) {
     if (is_user_logged_in() && !is_null($content) && !is_feed()) {
          return do_shortcode($content);
     }
     return '<p>请注册登录后查看内容</p>';
}
add_shortcode('member', 'member_check_shortcode');

然后在编辑文章是可以通过添加短代码,只有登录用户才能看到的内容

[member]登录后显示的内容[/member]

上面是原代码,现在美化一下

function member_check_shortcode($atts, $content = null) {
     if (is_user_logged_in() && !is_null($content) && !is_feed()) {
          return do_shortcode($content);
     }
     return '<h2>萝莉合集下载</h2><center><p class="ex" ><span style="color: #ff0000;">温馨提示:合集内容请注册登录后免费下载</span> <a href="https://****.com/yaoqingmakefu" target="_blank" > ( 点击注册 ) 开启宅男的快乐.avi</a></p></center>';
}
add_shortcode('member', 'member_check_shortcode');

相关文章