如果我们有在使用WordPress搭建个人博客的时候,一般很多网友都喜欢自己捣鼓各种功能。比如我们会自定义调用随机内容、热门文章帖子等信息。WordPress有些功能是自带的,有些是需要我们用插件和代码脚本实现的。老蒋在浏览网友博客的时候会看到有些博主在文章篇幅底部设置历史上的今天调出文章,这样还有比较有意思的,可以看到去年、前年的今天更新过哪些文章。
这个功能如何实现的呢?其实可以通过This Day In History插件实现,直接在后台搜索安装即可去设置调用。我们也可以使用无插件只用代码实现。
第一、脚本部分


//历史上的今天,代码来自柳城博主的WP-Today插件
function wp_today(){
global $wpdb;<br> $post_year = get_the_time('Y');
$post_month = get_the_time('m');<br> $post_day = get_the_time('j');
$sql = "select ID, year(post_date_gmt) as h_year, post_title, comment_count FROM<br> $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'<br> order by post_date_gmt DESC limit 5";<br> $histtory_post = $wpdb->get_results($sql);
if( $histtory_post ){<br> foreach( $histtory_post as $post ){<br> $h_year = $post->h_year;<br> $h_post_title = $post->post_title;<br> $h_permalink = get_permalink( $post->ID );<br> $h_comments = $post->comment_count;<br> $h_post .= "
  • $h_year:</strong>&nbsp;&nbsp;<a href='".$h_permalink."' title='".$h_post_title."' target='_blank'>$h_post_title($h_comments)</a></li>";<br> }<br> }<br> if ( $h_post ){
    $result = "<h2>历史上的今天:</h2><ul>".$h_post."";
    }
    return $result;<br> }<br> function wp_today_auto($content){
    if( is_single() ){
    $content = $content.wp_today();
    }
    return $content;
    }
    add_filter('the_content', 'wp_today_auto',9999);

  • 将代码加入到当前主题的Functions.php文件中。
    第二、调出历史文章

    <?php echo wp_today(); ?>

    在需要调出历史今天的位置加上上面调出代码。对于CSS样式我们自行设置添加。


    扫描二维码,在手机上阅读!