秋硕学习笔记 美化 WordPress通过插件/代码隐藏文章内容 需登录或输入密码可见

WordPress通过插件/代码隐藏文章内容 需登录或输入密码可见

一、通过安装插件的方式实现 推荐安装—Login to view all插件,实现使隐藏WordPress文章…

一、通过安装插件的方式实现

推荐安装—Login to view all插件,实现使隐藏WordPress文章部分内容,让用户登录后可见。

WordPress官方下载:https://wordpress.org/plugins/login-to-view-all/

云盘下载

插件本地下载地址(演示):

插件本地下载地址:https://xiazai.ludou.org/login-to-view-all.zip

二、通过代码方式实现

1、简单版

在主题的functions.php文件添加以下代码:

//部分内容登录可见
add_shortcode('hide','loginvisible');
function loginvisible($atts,$content=null){
    if(is_user_logged_in() && !is_null($content) && !is_feed())
    return $content;
    return '';
}

在编辑文章是使用短码包围要隐藏的内容:

[hide]
登陆才可以看到的内容
[/hide]

2、美化版

在主题function.php文件里加入以下代码。其中可用于直接将href=”#respond”后的“#respond”替换为自己站点的登录地址,以方便用户快速登录。

//部分内容登录可见
function login_to_read($atts, $content=null) {
extract(shortcode_atts(array("notice" => '
<span style="color: red;">温馨提示:</span>此处内容需要<a title="登录后可见" href="#respond">登录</a>后才能查看!
'), $atts));
if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
 return $content;
 return $notice;
}
add_shortcode('vip', 'login_to_read');

在编辑文章时,使用短码包围要隐藏的内容:

[vip]我是被隐藏的内容,样式一(默认样式)[/vip]
[vip] notice="登录后才显示哟"]我是被隐藏的内容,样式二(自定义回复信息)[/vip]

3、输入密码显示

首先在主题functions.php文件中添加下面代码:

//部分内容输入密码可见 蜗牛789 — www.wn789.com
function e_secret($atts, $content=null){
 extract(shortcode_atts(array('key'=>null), $atts));
 if(isset($_POST['e_secret_key']) && $_POST['e_secret_key']==$key){
 return '
<div class="e-secret">'.$content.'</div>
';
 }
 else{
 return '
<form class="e-secret" action="'.get_permalink().'" method="post" name="e-secret"><label>输入密码查看加密内容:</label><input type="password" name="e_secret_key" class="euc-y-i" maxlength="50"><input type="submit" class="euc-y-s" value="确定">
<div class="euc-clear"></div>
</form>
';
 }
}
add_shortcode('secret','e_secret');

第二步,在主题main.css样式文件里添加下面代码:

/*e-secret*/
.e-secret {
 margin: 20px 0;
 padding: 20px;
 background: #f8f8f8;
}
.e-secret input.euc-y-i[type="password"] {
 float: left;
 background: #fff;
 width: 100%;
 line-height: 36px;
 margin-top: 5px;
 border-radius: 3px;
}
.e-secret input.euc-y-s[type="submit"] {
 float: right;
 margin-top: -47px;
 width: 30%;
 margin-right: 1px;
 border-radius: 0 3px 3px 0;
}
input.euc-y-s[type="submit"]{background-color:#3498db;color:#fff;font-size:21px;box-shadow:none;-webkit-transition: .4s;-moz-transition: .4s;-o-transition: .4s;transition:.4s;-webkit-backface-visibility:hidden;position:relative;cursor:pointer;padding: 13px 20px;text-align: center;border-radius: 50px;-webkit-box-shadow: none; -moz-box-shadow: none; box-shadow: none;border: 0;height: auto;outline: medium;line-height: 20px;margin: 0;}
input.euc-y-s[type="submit"]:hover{background-color:#5dade2;}
input.euc-y-i[type="text"],input.euc-y-i[type="password"]{border:1px solid #F2EFEF;color:#777;display:block;background: #FCFCFC;font-size:18px;transition:all .5s ease 0;outline:0;box-sizing:border-box;-webkit-border-radius:25px;-moz-border-radius:25px;border-radius:25px;padding:5px 16px; margin: 0;height: auto;line-height: 30px;}
input.euc-y-i[type="text"]:hover,input.euc-y-i[type="password"]:hover{border:1px solid #56b4ef;box-shadow:0 0 4px #56b4ef;}

在编辑文章时,使用短码包围要隐藏的内容:

[secret key="密码"]
加密内容
[/secret]
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。若本站内容侵犯了原著者的合法权益,请联系我们进行处理。本文地址:https://wparticle.cn/427.html

作者: wordus

记录生活感悟,分享网络资源,交流学习体会,感受美好人生。秋硕学习笔记,记录分享学习、生活、工作、旅游、健身、爱好的个人博客。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

返回顶部