WordPress实现评论后可显示内容中附件下载地址的方法
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">本文实例讲述了WordPress实现评论后可显示内容中附件下载地址的方法。分享给大家供大家参考,具体如下:</p>
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
最近在做一个项目的时候,有个需求就是希望WordPress网站文章内容里面附件可以评论后才可以下载。网络上面查了会,发现这个功能不难实现,写个简单的函数就可以了。而且这样也可以设置部分文章评论后可见。觉得这个功能应该挺多人有需要的,索性也就写一篇wordpress文章内容回复后可见的教程。现在来说说如何实现wordpress的文章内容评论后可见吧?其实实现起来很简单,利用wordpress的短代码功能即可实现,代码如下:<br>
</p>
<div style="margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; font-size: 12px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden;">
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
<p>
代码如下:</p>
</div>
<div id="phpcode5" style="margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word;">
function reply_to_read($atts, $content=null) {<br>
extract(shortcode_atts(array("notice" => '温馨提示: 此处内容需要评论本文后才能查看.'), $atts));<br>
$email = null;<br>
$user_ID = (int) wp_get_current_user()->ID;<br>
if ($user_ID > 0) {<br>
$email = get_userdata($user_ID)->user_email;<br>
//对博主直接显示内容<br>
$admin_email = "xxx@aaa.com"; //博主Email<br>
if ($email == $admin_email) {<br>
return $content;<br>
}<br>
} else if (isset($_COOKIE['comment_author_email_' . COOKIEHASH])) {<br>
$email = str_replace('%40', '@', $_COOKIE['comment_author_email_' . COOKIEHASH]);<br>
} else {<br>
return $notice;<br>
}<br>
if (empty($email)) {<br>
return $notice;<br>
}<br>
global $wpdb;<br>
$post_id = get_the_ID();<br>
$query = "SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID`={$post_id} and `comment_approved`='1' and `comment_author_email`='{$email}' LIMIT 1";<br>
if ($wpdb->get_results($query)) {<br>
return do_shortcode($content);<br>
} else {<br>
return $notice;<br>
}<br>
}<br>
add_shortcode('reply', 'reply_to_read');</div>
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
</p>
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
1.需要注意的是,要修改第8行的邮件为管理员的。如果你的网站使用了ajax免刷新提交评论,应该还需要修改第2行的提示文字,提示访客评论后刷新页面来查看隐藏内容。</p>
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
2.编辑文章时,使用下面的简码:</p>
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
<font color="#0000ff" style="line-height: 25.2px;">【reply】评论可见的内容【/reply】</font></p>
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
或者</p>
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
<font color="#0000ff" style="line-height: 25.2px;">【reply notice="自定义的提示信息"】评论可见的内容【/reply】</font></p>
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
希望本文所述对大家基于wordpress的程序设计有所帮助。</p>
頁:
[1]