为了能更好地展示一些没有更新的老文章,我们可以在文章页面下添加一个“历史上的今天”功能获取老文章列表予以展示,如下图所示:
该功能代码取至柳城大佬的 wp-today 插件,具体实现方法将以下代码添加到function.php
文件末尾即可。
//历史上的今天,代码来自柳城博主的 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> <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);
上述代码默认是在文章结尾自动添加。
如果需要自定义显示位置,则只需去掉上述代码的以下部分。
function wp_today_auto($content){
if( is_single() ){
$content = $content.wp_today();
}
return $content;
}
add_filter('the_content', 'wp_today_auto',9999);
然后在需要显示的位置用下面函数调用即可。
<?php echo wp_today(); ?>
样式文件的话没有调整,大家根据自己喜好自行调整一下吧。
© 版权声明
这些信息可能会帮助到你: 下载帮助 | 免责说明 | 进站必看 | 广告投放
版权声明:本文采用知识共享 署名4.0国际许可协议 BY-NC-SA 4.0 进行授权文章名称:《为WordPress文章添加历史上的今天列表》
文章链接:https://www.jsksky.com/add-a-historical-list-of-todays-for-wordpress-articles.html
免责声明:根据《计算机软件保护条例》第十七条规定“为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。”您需知晓本站所有内容资源均来源于网络,仅供用户交流学习与研究使用,版权归属原版权方所有,版权争议与本站无关,用户本人下载后不能用作商业或非法用途,需在24个小时之内从您的电脑中彻底删除上述内容,否则后果均由用户承担责任;如果您访问和下载此文件,表示您同意只将此文件用于参考、学习而非其他用途,否则一切后果请您自行承担,如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
本站为个人博客非盈利性站点,所有软件信息均来自网络,所有资源仅供学习参考研究目的,并不贩卖软件,不存在任何商业目的及用途,部分软件收费或网站会员捐赠是您喜欢本站而产生的赞助支持行为,仅为维持服务器的开支与维护,全凭自愿无任何强求。
THE END
请登录后发表评论
注册
社交帐号登录