WordPress纯代码实现展示最近更新过的文章

如题,我们有时候会因为一些功能的不完美或者代码的时效性、软件主题的更新等,对一些旧文章进行一些不定期更新,但是这种更新一旦没有及时通知访客,往往就没有什么意义了,访客很难知道。之前小灰灰看过一篇文章《WordPress修改更新文章后邮件通知评论过那篇文章的用户》,小灰灰觉得这个方法也很好,但是也有其局限性,比如邮件太多会造成垃圾邮件、有限的评论访客导致文章更新扩散不广等等。而小灰灰这里提到的《WordPress纯代码实现展示最近更新过的文章》或许能有效的避免一些困扰吧,二者结合起来也可能更有效果。

1、代码部署

将以下代码添加到主题的 functions.php里,代码来自ZWWoOoOo

  1. function recently_updated_posts($num=10,$days=7) {
  2.    if( !$recently_updated_posts = get_option('recently_updated_posts') ) {
  3.        query_posts('post_status=publish&orderby=modified&posts_per_page=-1');
  4.        $i=0;
  5.        while ( have_posts() && $i<$num ) : the_post();
  6.            if (current_time('timestamp') - get_the_time('U') > 60*60*24*$days) {
  7.                $i++;
  8.                $the_title_value=get_the_title();
  9.                $recently_updated_posts.='<li><a href="'.get_permalink().'" title="'.$the_title_value.'">'
  10.                .$the_title_value.'</a><span class="updatetime"><br />&raquo; 修改时间: '
  11.                .get_the_modified_time('Y.m.d G:i').'</span></li>';
  12.            }
  13.        endwhile;
  14.        wp_reset_query();
  15.        if ( !emptyempty($recently_updated_posts) ) update_option('recently_updated_posts', $recently_updated_posts);
  16.    }
  17.    $recently_updated_posts=($recently_updated_posts == '') ? '<li>None data.</li>' : $recently_updated_posts;
  18.    echo $recently_updated_posts;
  19. }
  20. function clear_cache_zww() {
  21.     update_option('recently_updated_posts', ''); // 清空 recently_updated_posts
  22. }
  23. add_action('save_post', 'clear_cache_zww'); // 新发表文章/修改文章时触发更新

2、调用方法

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

  1. <h3>Recently Updated Posts</h3>
  2. <ul>
  3. <?php if ( function_exists('recently_updated_posts') ) recently_updated_posts(8,15); ?>
  4. </ul>
提示

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

图片演示效果

发现图片被小灰灰搞得有点模糊了,哈哈,失策,将就着看吧。

WordPress纯代码实现展示最近更新过的文章

放在侧边栏显示有更新的文章

WordPress纯代码实现展示最近更新过的文章

新建一个页面显示有更新的文章

《WordPress修改更新文章后邮件通知评论过那篇文章的用户》

开头小灰灰有提到这个方法,这里也顺便补充一下,大家也可以选择性使用,这个要慎用哦,代码来自龙笑天下
将以下代码添加到functions.php中,会在修改编辑文章的页面添加一个选项,只有勾选才会邮件通知。

  1. /**
  2.  * Wordpress 修改更新文章后邮件通知评论过那篇文章的用户 - 龙笑天下
  3.  * http://www.ilxtx.com/notify-comment-authors-when-the-post-is-updated.html
  4.  */
  5. //修改更新文章时邮件通知评论用户
  6. add_action( 'submitpost_box', 'lxtx_fo_submit_box');
  7. function lxtx_fo_submit_box( ){
  8. echo '<div id="fo_side-sortables" class="meta-box-sortables ui-sortable">';
  9. echo '<div id="fo_submit_box" class="postbox ">';
  10. echo '<div class="handlediv" title="点击以切换"><br></div>';
  11. echo '<h3 class="hndle"><span>邮件通知</span></h3>';
  12. echo '<div class="inside"><div class="submitbox">';
  13. 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>';
  14. echo '</div></div>';
  15. echo '</div>';
  16. echo '</div>';
  17. }
  18. //开始
  19. add_action( 'publish_post', 'lxtx_fo_emaill_report_users' );
  20. function lxtx_fo_emaill_report_users($post_ID)
  21. {
  22. //如果未勾选保存,不进行任何操作
  23. if($_POST['FO_emaill_report_user'] != 'true'){
  24. return;
  25. }
  26. //修订版本不通知,以免滥用
  27. if( wp_is_post_revision($post_ID) ){
  28. return;
  29. }
  30. //获取wp数据操作类
  31. global $wpdb,$post;
  32. // 读数据库,获取文章的所有用户的email并且不重复
  33. $emailauthor != '你自己的邮箱';
  34. $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");
  35. // 获取博客名称
  36. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  37. // 获取博客URL
  38. $blogurl = get_bloginfo("siteurl");
  39. //文章链接
  40. $post_link = get_permalink($post_ID);
  41. //文章标题$post -> post_title
  42. $post_title = strip_tags($_POST['post_title']);
  43. //文章内容$post->post_content
  44. $post_content = strip_tags($_POST['post_content']);
  45. //文章摘要
  46. $output = preg_replace('/^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,0}((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,200}).*/s','\1',$post_content).'......';
  47. //邮件头,以免乱码
  48. $message_headers = "Content-Type: text/html; charset=\"utf-8\"\n";
  49. // 邮件标题
  50. $subject = '您曾经来访过的《'.$post_title.'》文章更新通知。';
  51. foreach ( $wp_user_emails as $wp_user_email )
  52. {
  53. // 邮件内容
  54. $message = '
  55. <div style="MARGIN-RIGHT: auto; MARGIN-LEFT: auto;">
  56. <strong style="line-height: 1.5; font-family:Microsoft YaHei;">
  57. 亲爱的'.$wp_user_email->comment_author.':
  58. </strong>
  59. <p style="FONT-SIZE: 14px; PADDING-TOP: 6px">
  60. 您曾经来访过的《'.$post_title.'》有更新,博主觉得有必要通知您,希望不会骚扰到您。
  61. </p>
  62. <p style="FONT-SIZE: 14px; PADDING-TOP: 6px">
  63. 文章标题:<a title="'.$post_title.'" href="'.$post_link.'" target="_top">'.$post_title.'</a>
  64. <br/>
  65. 文章摘要:'.$output.'
  66. </p>
  67. <p style="FONT-SIZE: 14px; PADDING-TOP: 6px">
  68. 您可以点击链接
  69. <a href="'.$blogurl.'" style="line-height: 1.5;">'.$blogname.'</a>
  70. >
  71. <a title="'.$post_title.'" href="'.$post_link.'" target="_top">'.$post_title.'</a>
  72. 详细查看
  73. </p>
  74. <p style="font-size: 14px; padding-top: 6px; text-align: left;">
  75. <span style="line-height: 1.5; color: rgb(153, 153, 153);">
  76. 来自:
  77. </span>
  78. <a href="'.$blogurl.'" style="line-height: 1.5;">'.$blogname.'</a>
  79. </p>
  80. <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);">
  81. 该邮件为系统发送邮件,请勿直接回复!如有打扰,请向博主留言反映。灰常感谢您的阅读!
  82. </div>
  83. </div>';
  84. wp_mail($wp_user_email->comment_author_email, $subject$message$message_headers);
  85. }
  86. }

效果展示:
WordPress纯代码实现展示最近更新过的文章

 

觉得好的话记得打赏赞助小灰灰哦,小灰灰灰更有动力的,谢谢

小灰灰

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: