砥砺前行,蜗牛也上网
电脑及相关技术分享站

给Wordpress加个历史的今天

看很多博客站文章页下面有个“历史的今天”栏目,这会把你写该篇文章时往年同一天所写的文章列出来,感觉还是蛮有意思的。像这样

于是我也想给博客加一个,奈何本人码盲,只好做网络搬运工,找啊找,找啊找~~~~终于找到了2种可用的代码,但都提取自一位叫“柳城”的大佬的相关插件,这边把两份代码都附上。我自用的是第二份代码,另外本站用的是DUX主题

第一种
//历史上的今天,代码来自柳城博主的WP-Today插件

function wp_today(){

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 5";

$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><strong>$h_year:</strong>&nbsp;&nbsp;<a href='".$h_permalink."' title='".$h_post_title."' target='_blank'>$h_post_title($h_comments)</a></li>";

}

}

if ( $h_post ){

$result = "<h2>历史上的今天:</h2><ul>".$h_post."</ul>";

}

return $result;

}

function wp_today_auto($content){

if( is_single() ){

$content = $content.wp_today();

}

return $content;

}

add_filter('the_content', 'wp_today_auto',9999);

这份代码只要放到functions.php里就可以了,在文章中出现的位置是固定的,如果想要自定义在文章中的位置,则只要把上面代码中的

function wp_today_auto($content){

if( is_single() ){

$content = $content.wp_today();

}

return $content;

}

add_filter('the_content', 'wp_today_auto',9999);

这段删除或注释,另外在single.php中相应位置加入下面这行就OK

<?php?echo?wp_today();

效果图是这样的

所以,我还是喜欢第一种效果,带个框瞬间A格就上来了,扯远了,接着看第二种代码。

这份是来自“zibuyu"大佬的

先新建一个module_today_in_history.php文件放到主题目录下的modules目录里,代码奉上(在原版基础上适当做了点修改

<?php
//历史上的今天,代码来自柳城博主的 WP-Today 插件
function today_in_history(){

//$title = ZXM_options('today_in_history_title') ? ZXM_options('today_in_history_title') : "历史上的今天呀"; // $title = "历史上的今天";
//$limit = ZXM_options('today_in_history_num') ? ZXM_options('today_in_history_num') : 5; // $limit = 5;
// $title = "历史上的今天";
$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><lable>$h_year</lable>:<a href='".$h_permalink."' style='color:blue' title='Permanent Link to ".$h_post_title."'>$h_post_title <span>($h_comments)</span></a></li>";
}
}

$arr_month = array( 1=>"一月",2=>"二月",3=>"三月",4=>"四月",5=>"五月",6=>"六月",7=>"七月",8=>"八月",9=>"九月",10=>"十月",11=>"十一",12=>"十二" );
foreach($arr_month as $key => $value){
if($post_month == $key){
$month_l = $value;
}
}

if ( $h_post ){
$result = '
<section class="today-in-history">
<fieldset>
<legend>
<div class="today-date">
<span class="month">'.$month_l.'</span>
<span class="day">'.$post_day.'</span>
</div>
<h3>历史上的今天</h3>
</legend>
<ul>'.$h_post.'</ul>
</fieldset>
</section>';
}else{
$result = '<section class="today-in-history">
<fieldset>
<legend>
<div class="today-date">
<span class="month">'.$month_l.'</span>
<span class="day">'.$post_day.'</span>
</div> 
<h3>历史上的今天</h3>
</legend>
<ul>"吼吼~~~,往年的今天博主不知道跑哪里偷懒去了~~~"</ul>
</fieldset>
</section>';
}

echo $result;
}
today_in_history();
?>

然后在single.php里加入调用代码(放哪个位置自己定)

<?php include get_template_directory() . '/modules/module_today_in_history.php'; ?>

至此就已经达到了效果图2的状态了,然而我的要求是效果图1,加框这事是CSS的工作,然而网上都没给出CSS设置样式的代码,苦恼之下硬着头皮上,在经历了N次尝试、失败、放弃又尝试的循环后,终于在昨晚付出爆肝一副的代价后,偶成功了(这里先笑一分钟)。关键的CSS代码我给整出来了,直接丢main.css里就行。现双手奉上

刷新浏览器缓存,收工!效果图直接看文末。



微信扫描下方的二维码阅读本文

本站所有附件解压密码均为:zxmvps.com

未经允许不得转载!
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《给Wordpress加个历史的今天》
文章链接:https://www.zxmvps.com/archives/3746
本站文章均为佐罗日常工作经验总结手打,部分内容参考网上相关文章,可能存在少许相同之处,如侵犯了您的权益,请及时告之。
九月 22

历史上的今天

    吼吼~~~,往年的今天博主不知道跑哪里偷懒去了~~~

相关推荐

  • 暂无文章

请不吝赐教 抢沙发

  • Q Q (选填)
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址 (选填)

你的打赏,我的动力!

支付宝打赏

微信打赏

觉得文章有用就打赏一下文章作者