How to Show Posts of Historical ‘Today’ in WordPress?


You could show ‘Related’ posts (using tags, categories or other keywords) to attract more page views. Alternatively, you could show Historic Posts of ‘Today’ in the past at the end of the post content. Copy the following PHP code and put it at the end of the wordpress function template functions.php, preferably using child theme.

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');

The idea is to have a query that search for the posts and using add_filter to add to the the_content.

wordpress How to Show Posts of Historical 'Today' in Wordpress? wordpress

wordpress

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
291 words
Last Post: C++ Coding Exercise - Parallel For - Monte Carlo PI Calculation
Next Post: How to Show Random Posts in WordPress using Thumbnails?

The Permanent URL is: How to Show Posts of Historical ‘Today’ in WordPress?

Leave a Reply