WordPress教程

WordPress 添加历史上的今天文章列表

阿里云

之前写过一篇 WordPress 主题添加最新更新文章列表功能 的文章,可以很好地解决老文章更新后无法及时展示的问题。但是对于一些没有更新的老文章依旧不能很好地展示,特别是随着建站时间增长,一些之前写过的文章逐渐被埋没在历史的角落里。为了能更好地展示这些文章,我们可以在文章页面下添加一个“历史上的今天”功能获取老文章列表予以展示。

该功能代码取至柳城大佬的 wp-today 插件,该插件因年久失修已经在 WordPress 下架了。不过我从网上搜到的版本经测试依旧能用,文末会提供下载。在原有插件代码的基础上,我略微调整了下,并针对 OptionsFrameWork 后台框架,做了些设置选项。(注意:历史上的今天功能,仅针对建站一年以上的网站有效,时间太短的网站也就不存在历史上的今天了不是吗?ㄟ( ▔, ▔ )ㄏ )。

使用方法:

新建一个名为 module_today_in_history.php 的文件,然后将以下代码丢到改文件中。

也想出现在这里?联系我们
创客主机
  1. <?php
  2. //历史上的今天,代码来自柳城博主的 WP-Today 插件
  3. function today_in_history(){
  4.  
  5. 	$title = QGG_options('today_in_history_title');    // $title = "历史上的今天";          其他主题用户改成固定值
  6. 	$limit = QGG_options('today_in_history_num');    // $limit = 5;          其他主题用户改成固定值
  7.  
  8. 	global $wpdb;
  9. 	$post_year = get_the_time('Y');
  10. 	$post_month = get_the_time('m');
  11. 	$post_day = get_the_time('j');
  12.  
  13. 	$sql = "select ID, year(post_date_gmt) as h_year, post_title, comment_count FROM 
  14. 			$wpdb->posts WHERE post_password = '' AND post_type = 'post' AND post_status = 'publish'
  15. 			AND year(post_date_gmt)!='$post_year' AND month(post_date_gmt)='$post_month' AND day(post_date_gmt)='$post_day'
  16. 			order by post_date_gmt DESC limit $limit";
  17. 	$histtory_post = $wpdb->get_results($sql);
  18. 	if( $histtory_post ){
  19. 		foreach( $histtory_post as $post ){
  20. 			$h_year = $post->h_year;
  21. 			$h_post_title = $post->post_title;
  22. 			$h_permalink = get_permalink( $post->ID );
  23. 			$h_comments = $post->comment_count;
  24. 			$h_post .= "<li>$h_year:&nbsp;&nbsp;<a href='".$h_permalink."' title='Permanent Link to ".$h_post_title."'>$h_post_title <span>($h_comments)</span></a></li>";
  25. 		}
  26. 	}
  27.  
  28. 	if ( $h_post ){
  29. 		$result = "<section class='history-in-today'><h2>".$title."</h2><div><ul>".$h_post."</ul></div></section>";
  30. 	}else{
  31. 		$result = "<section class='history-in-today'><h2>".$title."</h2><div>哇哦~~~,历史上的今天没发表过文章哦</div></section>";
  32. 	}
  33.  
  34. 	echo $result;
  35. }
  36. today_in_history();
  37. ?>

调用代码

然后在你需要调用的位置添加如下代码,一般是主题的 single.php 文件中。

  1. <?php 
  2. // 历史上的今天功能
  3. if( QGG_options('today_in_history_open') ){
  4. 	include get_stylesheet_directory(). '/diy/modules/module_today_in_history.php';					
  5. }
  6. ?>

其他主题的话,如果不需要后台设置选项,去掉 if 判断即可。

设置选项

最后,对于使用 OptionsFramework 框架的主题用户,可在 options.php 文件中添加如下代码设置后台选项。

  1. <?php
  2. 	$options[] = array(
  3. 		'name' => __('蝈蝈文章', 'QGG'),
  4. 		'type' => 'heading' );
  5. 	// 文章页历史上的今天
  6. 	$options[] = array(
  7. 		'name' => __('历史上的今天', 'QGG'),
  8. 		'desc' => __('开启', 'QGG'),
  9. 		'id' => 'today_in_history_open',
  10. 		'std' => true,
  11. 		'type' => 'checkbox');
  12.  
  13. 	$options[] = array(
  14. 		'name' => __('历史上的今天-标题文字', 'QGG'),
  15. 		'desc' => __('左上角的标题文字', 'QGG'),
  16. 		'id' => 'today_in_history_title',
  17. 		'std' => __('历史上的今天', 'QGG'),
  18. 		'type' => 'text');
  19.  
  20. 	$options[] = array(
  21. 		'name' => __('历史上的今天-显示文章数', 'QGG'),
  22. 		'desc' => __('纯数字,显示列表文章数量。不明白?<a href="https://blog.quietguoguo.com">点击这里</a> 进行留言。', 'QGG'),
  23. 		'id' => 'today_in_history_num',
  24. 		'std' => 5,
  25. 		'class' => 'mini',
  26. 		'type' => 'text');			
  27.  
  28. ?>

样式文件的话没有调整,大家根据自己喜好自行调整一下吧。

WordPress 添加历史上的今天文章列表

已有 371 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!