PIX主题文章末尾添加历史上的今天

WP文章后面加上“历史上的今天”是一个很古老的功能,我说的历史上的今天,不是世界或者我国历史上发生了什么事情,而是历史上本站今天曾发布了哪些文章。

PIX主题文章末尾添加历史上的今天-似水流年

这个功能网上很多,我都忘记什么时候加的了,今天早上发布“堵车”这篇文章的时候忽然末尾冒了出来,我一看,但是采用的是柳城博主的插件改编的全代码版,但是显示的布局和PIX主题不相适应,很不好看。

本站为了和下面文章出现的效果保持一致,即圆角灰框,同一个背景色,就对柳城博主的代码进行了再加工。

文章正文前后添加同一分类目录下文章提示功能(二)只显示与它相邻的前3篇和后3篇文章 文章正文前后添加同一分类目录下文章提示功能(二)只显示与它相邻的前3篇和后3篇文章 本文是以下文章的衍生版本: [pix_post ids=250] 刚开始建站的时候,考虑到文章稀少,没有什么可读性,为了提高网站粘度,在文章前后插入了同类分类目录下的所有文章。随着文章越来越多,如果不... 时间:2023/11/21 分类:技术相关
PIX主题文章末尾添加历史上的今天-似水流年

把以下代码放到主题目录下的fuctions.php或func.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);
    $h_post = '';
    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 style='list-style-type: none;'><strong>$h_year:</strong>&nbsp;&nbsp;<a href='".$h_permalink."' title='".$h_post_title."' target='_blank'>$h_post_title($h_comments)</a></li>";
        }
    }
    $result = '';
    if ($h_post) {
        $result = "<div style='border:1px dashed #ccc;padding:10px;border-radius:5px;background-color:#f7f1e3;'><h2 style='font-size:16px;color:red;'>历史上本站今天曾发布的文章:</h2><ul style='list-style-type: none; padding-left:0; border-left:none;'>".$h_post."</ul></div>";
    }
    return $result;
}

function wp_today_auto($content) {
    if (is_single()) {
        $content = $content.wp_today();
    }
    return $content;
}
add_filter('the_content', 'wp_today_auto', 9999);

温馨提示: 此处为隐藏内容,需要评论本文后才能查看.
评论后请刷新一次页面,但注意不要清除缓存,否则还需要再次评论!

收工大吉!

历史上本站今天曾发布的文章:

Comments | 2 条评论
  • obaby

    Google Chrome 126 Google Chrome 126 cn中国–山东–临沂 联通 ip address 39.89.*.*

    感觉各种站内跳转,貌似也没啥人点

    • Avatar photo

      似水流年

      Microsoft Edge 126 Microsoft Edge 126 cn中国–河南–漯河 联通 ip address 42.238.*.*

      我就是为了写一篇文章,说不定过几天腻了就把代码恢复了。😂

消息盒子
# 您需要首次评论以获取消息 #
# 您需要首次评论以获取消息 #

只显示最新10条未读和已读信息