Add further reading list after post

Further reading content should be based on the post category in order to get the title and permalinks of the related posts.

Add further reading list after post

First approach

We could use filter to change the post contents. But it does not work as expected.

The result is the new contents are added on each paginated page, and break the pagination link block.

function my_content_filter($content) {
    $content .= further_reading();
    return $content;

function stringInsert($str,$insertstr,$pos) {
    $str = substr($str, 0, $pos) . $insertstr . substr($str, $pos);
    return $str;
add_filter( 'the_content', 'my_content_filter', 1 ); //set priority to 1 in order to add the new content right after post content from database.

Add further reading list after post

Second approach

The alternative way is to modify paging plugin directly.



File to change:


Inside paging plugin, it uses ‘the_post‘ filter to make pagination.

What we need to do is to change the post content directly in the $post object in filter_the_posts function.

$the_post->post_content .= further_reading();
$content = $the_post->post_content;

Avoid plugin auto-update

In order to keep the custom changes we need to avoid the plugin update.
Change the plugin version number to a big number. Add 9 prefix in this case.

Version: 90.2

further_reading function

function further_reading(){
    global $wpdb, $post;
    $post_id = get_the_ID();

    $categories = get_the_category();
    $category_id = $categories[0]->cat_ID;
    $slug = $categories[0]->slug;

    $args = array( 'posts_per_page' => 6,
    'category_name' => $categories[0]->slug );
    $myposts = get_posts( $args );

    $max = 0;
    foreach ( $myposts as $pst ){
    $len = strlen(get_the_title($pst));
    $max = max($len, $max);
    $width = 350;
    if($max>50) $width = 400;
    if($max>60) $width = 450;
    if($max>70) $width = 500;
    if($max>80) $width = 550;

    <div style="margin: 0; width: <?php echo $width ?>px;">
    <div style="float: left;"><p style="font-size:16px;font-weight:bold;">延伸阅读</p></div>
    <div style="float: right;margin:20px 0;">

    foreach ( $myposts as $pst ){
    setup_postdata( $pst );
    if($pst->ID == $post_id) continue;
        <a href="<?php echo get_permalink($pst) ?>" target="_blank" rel="noopener noreferrer"><?php echo get_the_title($pst); ?></a><?php //echo $pst->ID.'-'.$post_id; //echo strlen(get_the_title($pst)); ?><br>
    } ?>
    <div style="clear: all;"></div>
    $text = ob_get_contents();
    return $text;

 246 total views

Author: Albert

Leave a Reply