前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WordPress展示最近更新过的文章,并用邮件通知评论过的用户

WordPress展示最近更新过的文章,并用邮件通知评论过的用户

作者头像
杨逸轩
发布2018-06-14 10:13:10
5570
发布2018-06-14 10:13:10
举报
文章被收录于专栏:杨逸轩 ' sBlog杨逸轩 ' sBlog

展示更新文章代码

将以下代码添加到主题的 functions.php里

代码语言:javascript
复制
/** 
* WordPress展示最近更新过的文章 并通知评论过的用户 
* https://www.dujin.org/fenxiang/wp/9809.html 
*/ 
//展示最近更新过的文章 
function recently_updated_posts($num=10,$days=7) { 
 if( !$recently_updated_posts = get_option('recently_updated_posts') ) { 
 query_posts('post_status=publish&orderby=modified&posts_per_page=-1'); 
 $i=0; 
 while ( have_posts() && $i<$num ) : the_post(); 
 if (current_time('timestamp') - get_the_time('U') > 60*60*24*$days) { 
 $i++; 
 $the_title_value=get_the_title(); 
 $recently_updated_posts.='<li><a href="'.get_permalink().'" title="'.$the_title_value.'">' 
 .$the_title_value.'</a><span class="updatetime"><br />&raquo; 修改时间: ' 
 .get_the_modified_time('Y.m.d G:i').'</span></li>'; 
 } 
 endwhile; 
 wp_reset_query(); 
 if ( !empty($recently_updated_posts) ) update_option('recently_updated_posts', $recently_updated_posts); 
 } 
 $recently_updated_posts=($recently_updated_posts == '') ? '<li>None data.</li>' : $recently_updated_posts; 
 echo $recently_updated_posts; 
} 
 
function clear_cache_zww() { 
 update_option('recently_updated_posts', ''); // 清空 recently_updated_posts 
} 
add_action('save_post', 'clear_cache_zww'); // 新发表文章/修改文章时触发更新

调用方式

8 为展示文章数量,15 指15天内发表的文章除外,具体使用的时候可以根据自己的情况修改这两个参数。

代码语言:javascript
复制
<h3>Recently Updated Posts</h3> 
<ul> 
<?php if ( function_exists('recently_updated_posts') ) recently_updated_posts(8,15); ?> 
</ul>

添加数据库缓存方式,修改文章/删除文章/发表文章时才会更新缓存。 相关参数说明:$num – 展示数量,$days – 几天内的新文章除外。

更新文章后邮件通知评论过的用户

将以下代码添加到functions.php中,会在修改编辑文章的页面添加一个选项,只有勾选才会邮件通知。

代码语言:javascript
复制
/** 
 * WordPress展示最近更新过的文章 并通知评论过的用户 
 * https://www.dujin.org/fenxiang/wp/9809.html 
 */ 
//修改更新文章时邮件通知评论用户 
add_action( 'submitpost_box', 'lxtx_fo_submit_box'); 
function lxtx_fo_submit_box( ){ 
echo '<div id="fo_side-sortables" class="meta-box-sortables ui-sortable">'; 
echo '<div id="fo_submit_box" class="postbox ">'; 
echo '<div class="handlediv" title="点击以切换"><br></div>'; 
echo '<h3 class="hndle"><span>邮件通知</span></h3>'; 
echo '<div class="inside"><div class="submitbox">'; 
echo ' <div style="padding: 10px 10px 0;text-align: left;"><label class="selectit" title="慎用此功能,重要文章才勾选嘛,以免引起读者反感哈"><input type="checkbox" name="FO_emaill_report_user" value="true" title="勾选此项,将邮件通知本文所有评论者"/>邮通知本文所有评论者</label></div>'; 
echo '</div></div>'; 
echo '</div>'; 
echo '</div>'; 
} 
//开始 
add_action( 'publish_post', 'lxtx_fo_emaill_report_users' ); 
function lxtx_fo_emaill_report_users($post_ID) 
{ 
//如果未勾选保存,不进行任何操作 
if($_POST['FO_emaill_report_user'] != 'true'){ 
return; 
} 
 
//修订版本不通知,以免滥用 
if( wp_is_post_revision($post_ID) ){ 
return; 
} 
 
//获取wp数据操作类 
global $wpdb,$post; 
// 读数据库,获取文章的所有用户的email并且不重复 
$emailauthor != '你自己的邮箱'; 
$wp_user_emails = $wpdb->get_results("SELECT DISTINCT comment_author, comment_author_email FROM $wpdb->comments WHERE TRIM(comment_author_email) IS NOT NULL AND TRIM(comment_author_email) != '' AND TRIM(comment_author_email) != '$emailauthor' AND comment_post_ID = $post->ID"); 
 
// 获取博客名称 
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); 
// 获取博客URL 
$blogurl = get_bloginfo("siteurl"); 
 
//文章链接 
$post_link = get_permalink($post_ID); 
//文章标题$post -> post_title 
$post_title = strip_tags($_POST['post_title']); 
//文章内容$post->post_content 
$post_content = strip_tags($_POST['post_content']); 
//文章摘要 
$output = preg_replace('/^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,0}((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,200}).*/s','\1',$post_content).'......'; 
 
//邮件头,以免乱码 
$message_headers = "Content-Type: text/html; charset=\"utf-8\"\n"; 
// 邮件标题 
$subject = '您曾经来访过的《'.$post_title.'》文章更新通知。'; 
 
foreach ( $wp_user_emails as $wp_user_email ) 
{ 
// 邮件内容 
$message = ' 
<div style="MARGIN-RIGHT: auto; MARGIN-LEFT: auto;"> 
<strong style="line-height: 1.5; font-family:Microsoft YaHei;"> 
亲爱的'.$wp_user_email->comment_author.': 
</strong> 
<p style="FONT-SIZE: 14px; PADDING-TOP: 6px"> 
您曾经来访过的《'.$post_title.'》有更新,博主觉得有必要通知您,希望不会骚扰到您。 
</p> 
<p style="FONT-SIZE: 14px; PADDING-TOP: 6px"> 
文章标题:<a title="'.$post_title.'" href="'.$post_link.'" target="_top">'.$post_title.'</a> 
<br/> 
文章摘要:'.$output.' 
</p> 
<p style="FONT-SIZE: 14px; PADDING-TOP: 6px"> 
您可以点击链接 
<a href="'.$blogurl.'" style="line-height: 1.5;">'.$blogname.'</a> 
> 
<a title="'.$post_title.'" href="'.$post_link.'" target="_top">'.$post_title.'</a> 
详细查看 
</p> 
<p style="font-size: 14px; padding-top: 6px; text-align: left;"> 
<span style="line-height: 1.5; color: rgb(153, 153, 153);"> 
来自: 
</span> 
<a href="'.$blogurl.'" style="line-height: 1.5;">'.$blogname.'</a> 
</p> 
<div style="font-size: 12px; border-top-color: rgb(204, 204, 204); border-top-width: 1px; border-top-style: solid; height: 35px; width: 500px; color: rgb(102, 102, 102); line-height: 35px; background-color: rgb(245, 245, 245);"> 
该邮件为系统发送邮件,请勿直接回复!如有打扰,请向博主留言反映。灰常感谢您的阅读! 
</div> 
</div>'; 
wp_mail($wp_user_email->comment_author_email, $subject, $message, $message_headers); 
} 
}

在WordPress后台编辑文章页面右上方出,有“邮件通知本文所有评论者”。 本吊不用WP就不给你们测试了, 转载至:缙哥哥

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-01-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档