WordPress设置文章内容登录后可见

最近有人找我做网站,提到了一个需求,为了增加网站对用户的粘性,想让下载的链接登录后才能查看,这样可以增加网站的用户数量。

之前写过通过密码保护文章内容微信获取验证码查看文章内容,这里呢,简单的说下怎么实现文章内容登录后查看隐藏内容

1.在主题文件夹的functions.php文件中加入以下代码:

 /**
 * WordPress 只允许已登录的用户查看文章内容
 */
add_shortcode( 'members_only', 'members_only_shortcode' );
function members_only_shortcode( $atts, $content = null ) 
{
    if ( is_user_logged_in() && !empty( $content ) && !is_feed() )
    {
        return $content;
    }
 
    return '要查看文章内容,请先登录。';
} 

2.写文章时,在文章页面添加代码、段落、预格式化等格式都是可以的

代码格式效果如下:

[members_only]https://starcheng.xyz/216.html[/members_only] 

段落格式效果如下:

[members_only]https://starcheng.xyz/216.html[/members_only]

列表格式效果下入:

  1. [members_only]https://starcheng.xyz/216.html[/members_only]
  2. [members_only]https://starcheng.xyz/216.html[/members_only]
  3. [members_only]https://starcheng.xyz/216.html[/members_only]
 收藏 (0) 打赏

您可以选择一种方式赞助本站

支付宝扫一扫赞助

微信钱包扫描赞助

转载请注明出处橙苏博客 » WordPress设置文章内容登录后可见
分享到: 生成海报

本站接广告推广、脚本定制、付费咨询

联系微信:Saynothing2020
切换注册

登录

忘记密码 ?

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活