对于隐藏内容Wordpress本身有一个自带的加密功能,就在编辑栏邮编“发布”按钮的上班有一个公开度,编辑它就能,设置为输入密码浏览,缺点是整篇文章都看不到任何内容,不人性化,下边讲下几种可以隐藏部分内容的方法。
在主题模板函数 functions.php的<?php下边添加代码实现。
一、隐藏部分内容,需要输入密码可见
加密是一个很强大的功能,在很多时候都会用到它,比如碰到不和谐的内容,不方便被搜索引擎收录。
//为部分内容加密
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="post-password-form" action="'.get_permalink().'" method="post" name="e-secret"><p><label for="pwbox-142">输入密码查看加密内容: <input type="password" name="e_secret_key" size="20" /></label> <input type="submit" class="euc-y-s" value="确定" /></p> </form> '; } } add_shortcode('secret','e_secret');
在需要加密的内容头底加入下边两段代码,把123456可以改成你自己的密码。
[secret key=”123456″]
这里是要加密的内容。
[/secret]
二、隐藏部分内容,需要登录网站可见
这种方式适合VIP会员,会员要注册登录才能访问,而注册需要邀请码,可以出售邀请码,就变成了VIP会员。
// 添加只允许登录后查看
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; } $a= '<center><span> <div style="text-align:center;border:1px dashed #FF9A9A;padding:8px;margin:10px auto;color:green;">要查看更多文章内容,请您先<a href="http://www.yongyout3.com/wp-login.php" target="_blank">登录/注册</a> </div> </span></center>'; echo $a; }
在需要加密的内容头底加入下边两段代码,就会提示只有登录后才能访问隐藏内容,你就等着卖VIP赚大钱把。
[members_only]
这里的内容只为已登录的用户显示的
[/members_only]
三、隐藏部分内容,回复可见
这个功能更有粘度,参与性比较强,读者喜欢的内容想看到,就必须发表自己的评论。
function reply_to_read($atts, $content=null) { extract(shortcode_atts(array("notice" => '<p class="reply-to-read">温馨提示: 此处内容需要<a href="#respond" title="评论本文">评论本文</a>后才能查看.</p>'), $atts)); $email = null; $user_ID = (int) wp_get_current_user()->ID; if ($user_ID > 0) { $email = get_userdata($user_ID)->user_email; //对博主直接显示内容 $admin_email = "xxx@aaa.com"; //博主Email if ($email == $admin_email) { return $content; } } else if (isset($_COOKIE['comment_author_email_' . COOKIEHASH])) { $email = str_replace('%40', '@', $_COOKIE['comment_author_email_' . COOKIEHASH]); } else { return $notice; } if (empty($email)) { return $notice; } global $wpdb; $post_id = get_the_ID(); $query = "SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID`={$post_id} and `comment_approved`='1' and `comment_author_email`='{$email}' LIMIT 1"; if ($wpdb->get_results($query)) { return do_shortcode($content); } else { return $notice; } } add_shortcode('reply', 'reply_to_read');
在文章内容中,需要隐藏的内容放在两段代码中间即可,注意,在使用下边代码时,去掉@。
[@reply]
评论后可见的内容
[@/reply]
非常给力吧,赶紧设置一下,只要是中间的东西都能被隐藏,包括文字、图片、代码等内容。对于一些资源性质的网站,出售VIP会员是个不错的选择。
评论前必须登录!
注册