如何在文章最后显示 历史上的今天 [WordPress]?


一般来说, 可以通过 显示相关文章来 增加页面浏览量 Page Views. 另一种方法就是显示 历史上的今天. 把下面PHP代码复制一份到 函数模板 functions.php 的最后. 推荐在子主题里修改这样每次主题更新你就不需要重新再复制了.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function today_in_histroy(){
    $today = getdate();
    $args = array(
        'date_query' => array(
            array(
                'year'      => $today['year'],
                'compare'   => '!=',
            ),
            array(
                'month' => $today['mon'],
                'day'   => $today['mday'],
            ),
        ),
    );
    $postlist = get_posts($args);
    $html = '<h3 class="tih-title">Posts of Historic Today</h3><ul class="tih-title-list">';
    if(!empty($postlist)){
        foreach ($postlist as $key => $post) {
            $html .= '<li class="tih-title-item"><a href="' . get_permalink($post->ID) . '" title="' . $post->post_title . '">' . $post->post_title . '</a></li>';
        }
        $html .= '</ul>';
        return $html;
    }
    return "";
}
 
function add_today_in_histroy($content){
    global $post;
    return  $content . today_in_histroy(); 
}
add_filter('the_content','add_today_in_histroy');
function today_in_histroy(){
    $today = getdate();
    $args = array(
        'date_query' => array(
            array(
                'year'      => $today['year'],
                'compare'   => '!=',
            ),
            array(
                'month' => $today['mon'],
                'day'   => $today['mday'],
            ),
        ),
    );
    $postlist = get_posts($args);
    $html = '<h3 class="tih-title">Posts of Historic Today</h3><ul class="tih-title-list">';
    if(!empty($postlist)){
        foreach ($postlist as $key => $post) {
            $html .= '<li class="tih-title-item"><a href="' . get_permalink($post->ID) . '" title="' . $post->post_title . '">' . $post->post_title . '</a></li>';
        }
        $html .= '</ul>';
        return $html;
    }
    return "";
}

function add_today_in_histroy($content){
    global $post;
    return  $content . today_in_histroy(); 
}
add_filter('the_content','add_today_in_histroy');

原理很简单 就是通过查询 数据库里 日期和今天 一样的帖子 并用 add_filter 函数把 HTML代码给 更新到文章内容变量 the_content 里.

wordpress 如何在文章最后显示 历史上的今天 [Wordpress]? PHP是最好的语言 wordpress 互联网 小技巧 程序设计 编程 网站信息与统计

wordpress

英文: 如何在文章最后显示 历史上的今天 [Wordpress]?

Wordpress博客技术文章

GD Star Rating
loading...
本文一共 157 个汉字, 你数一下对不对.
如何在文章最后显示 历史上的今天 [WordPress]?. (AMP 移动加速版本)
上一篇: 剑桥 U-寿司
下一篇: 如何显示缩略图 随机文章 WordPress?

扫描二维码,分享本文到微信朋友圈
63f3a13c00ccdfbccbefc74494fca9ad 如何在文章最后显示 历史上的今天 [Wordpress]? PHP是最好的语言 wordpress 互联网 小技巧 程序设计 编程 网站信息与统计

4 条评论

评论