^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助,如果有代码出错,请联系站长解决
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 腾讯云3年2核2G新品轻量限时特惠只需408元

WordPress插件推荐:WP-Today

A wordpress plugin to display the posts of today on history. 显示 wordpress 博客往年今天的文章。

插件功能很简单,但很实用,博客文章越多,建站超过 1 年,才能看到最棒的效果,陌小雨推荐使用(正在使用中)。

2 核 2G 限时特惠 396 元/3 年    宝塔建站 10850 大礼包

插件安装方式:直接在后台搜索WP-Today即可。

插件可以自定义前台输出样式。

2016 年 5 月 12 日更新

纯代码版本实现历史上的今天:

    //历史上的今天,代码来自柳城博主的 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);

上述代码默认是在文章结尾自动添加。

如果需要自定义显示位置,则只需去掉上述代码的这个部分

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(); ?>

赠人玫瑰,手有余香。