WordPress添加历史上的今天文章列表功能
本文最后更新于 660 天前,文中的信息可能已经有所变化。如有误,请留言反馈。

随着时间推移,之前发布的文章逐渐被深藏,难被展现到访问者的眼前。为了让这些老文章也出现在访问者的视野里,我们可以添加历史上的今天文章列表功能,获取的是往年的今天发布的文章。

实现步骤:


  • 新建一个名为 module_today_in_history.php 的文件,加入以下代码:
<?php
//历史上的今天,代码来自柳城博主的 WP-Today 插件
function today_in_history(){

$title = QGG_options('today_in_history_title'); // $title = "历史上的今天"; 其他主题用户改成固定值
$limit = QGG_options('today_in_history_num'); // $limit = 5; 其他主题用户改成固定值

global $wpdb;
$post_year = get_the_time('Y');
$post_month = get_the_time('m');
$post_day = get_the_time('j');

$sql = "select ID, year(post_date_gmt) as h_year, post_title, comment_count FROM 
$wpdb->posts WHERE post_password = '' AND post_type = 'post' AND post_status = 'publish'
AND year(post_date_gmt)!='$post_year' AND month(post_date_gmt)='$post_month' AND day(post_date_gmt)='$post_day'
order by post_date_gmt DESC limit $limit";
$histtory_post = $wpdb->get_results($sql);
if( $histtory_post ){
foreach( $histtory_post as $post ){
$h_year = $post->h_year;
$h_post_title = $post->post_title;
$h_permalink = get_permalink( $post->ID );
$h_comments = $post->comment_count;
$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>";
}
}

if ( $h_post ){
$result = "<section class='history-in-today'><h2>".$title."</h2><div><ul>".$h_post."</ul></div></section>";
}else{
$result = "<section class='history-in-today'><h2>".$title."</h2><div>哇哦~~~,历史上的今天没发表过文章哦</div></section>";
}

echo $result;
}
today_in_history();
?>
  • 一般是主题的single.php文件中,在需要调用的位置添加如下代码:
<?php 
// 历史上的今天功能
if( QGG_options('today_in_history_open') ){
include get_stylesheet_directory(). '/diy/modules/module_today_in_history.php'; 
}
?>

如果不需要后台设置选项,去掉 if 判断即可。

  • 使用 OptionsFramework 框架的主题用户,可在 options.php 文件中添加如下代码设置后台选项:
<?php
$options[] = array(
'name' => __('蝈蝈文章', 'QGG'),
'type' => 'heading' );
// 文章页历史上的今天
$options[] = array(
'name' => __('历史上的今天', 'QGG'),
'desc' => __('开启', 'QGG'),
'id' => 'today_in_history_open',
'std' => true,
'type' => 'checkbox');

$options[] = array(
'name' => __('历史上的今天-标题文字', 'QGG'),
'desc' => __('左上角的标题文字', 'QGG'),
'id' => 'today_in_history_title',
'std' => __('历史上的今天', 'QGG'),
'type' => 'text');

$options[] = array(
'name' => __('历史上的今天-显示文章数', 'QGG'),
'desc' => __('纯数字,显示列表文章数量。不明白?<a href="https://blog.quietguoguo.com">点击这里</a> 进行留言。', 'QGG'),
'id' => 'today_in_history_num',
'std' => 5,
'class' => 'mini',
'type' => 'text'); 

?>
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇