WordPress 添加历史上的今天教程 WP 主题优化增强 function 文件实用功能代码段

ximagine
ximagine
站长
2082
文章
1
粉丝
教程 说明书评论134字数 512阅读1分42秒阅读模式
宝藏摘要WordPress 可以通过显示相关文章来增加页面浏览量,另一种方法就是显示历史上的今天,可以看到更早之前同一天更新过哪些文章,原理就是通过查询数据库里日期和今天 一样的文章,并用...
荒岛广告位招商进行中...
WordPress 添加历史上的今天教程 WP 主题优化增强 function 文件实用功能代码段
日期:2023年9月13日 分类:教程  说明书 评论:发表 浏览:134

WordPress 可以通过显示相关文章来增加页面浏览量,另一种方法就是显示历史上的今天,可以看到更早之前同一天更新过哪些文章,原理就是通过查询数据库里日期和今天 一样的文章,并用 add_filter 函数把 HTML 代码更新到文章内容变量 the_content 里,荒岛本次带来 WordPress 添加历史上的今天教程,只要将下面的代码添加到主题的「functions.php」文件即可。

  1. // 荒岛一座藏有宝藏的小岛
  2. function hd_today(){
  3. global $wpdb;
  4. $today_post = '';
  5. $result = '';
  6. $post_year = get_the_time('Y');
  7. $post_month = get_the_time('m');
  8. $post_day = get_the_time('j');
  9. $sql = "select ID, year(post_date_gmt) as today_year, post_title, comment_count FROM
  10. $wpdb->posts WHERE post_password = '' AND post_type = 'post' AND post_status = 'publish'
  11. AND year(post_date_gmt)!='$post_year' AND month(post_date_gmt)='$post_month' AND day(post_date_gmt)='$post_day'
  12. order by post_date_gmt DESC limit 7";
  13. $histtory_post = $wpdb->get_results($sql);
  14. if( $histtory_post ){
  15. foreach( $histtory_post as $post ){
  16. $today_year = $post->today_year;
  17. $today_post_title = $post->post_title;
  18. $today_permalink = get_permalink( $post->ID );
  19. $today_post .= '<li><a href="'.$today_permalink.'" target="_blank"><span>'.$today_year.'</span>'.$today_post_title.'</a></li>';
  20. }
  21. }
  22. if ( $today_post ){
  23. $result = '<div class="begin-today"><fieldset><legend><h5>'. sprintf(__( '历史上的今天', 'begin' )) .'</h5></legend><div class="today-date"><div class="today-m">'.get_the_date( 'F' ).'</div><div class="today-d">'.get_the_date( 'j' ).'</div></div><ul>'.$today_post.'</ul></fieldset></div>';
  24. }else{
  25. $result = '<div class="begin-today"><fieldset><legend><h5>'. sprintf(__( '历史上的今天', 'begin' )) .'</h5></legend><div class="today-date"><div class="today-m">'.get_the_date( 'F' ).'</div><div class="today-d">'.get_the_date( 'j' ).'</div></div><div><span class="nfhd">'.get_the_time('Y').'</span>&nbsp;&nbsp;荒岛宝藏 即将发布 敬请期待...</div></fieldset></div>';
  26. }
  27. return $result;
  28. }
宝藏内容来自荒岛-https://x-imagine.com/wordpress-today-in-history.html宝藏内容来自荒岛-https://x-imagine.com/wordpress-today-in-history.html

请按「Ctrl+D」收藏荒岛分享给好友,如您发现本文教程或宝藏链接已失效,请联系站长修正!

荒岛公众号
扫一扫关注
weinxin
HDsoft27
公众号ID已复制,前往微信关注...
荒岛哔哩姬
扫一扫关注
weinxin
ximagine
哔哩姬ID已复制,前往B站关注...
荒岛广告位招商进行中...
ximagine
荒岛广告位招商进行中...

发表评论