Library Technology - Reviews, Tips, Giveaways, Freeware

Library Technology – Reviews, Tips, Giveaways, Freeware

How to add “Related posts” in content of WordPress blog?

Posted In Wordpress plugins, Wordpress tips - By Techtiplib on Tuesday, October 7th, 2014 With No Comments »

To show related articles you can use the plugin Better Related Posts, Yet Another Related Posts Plugin… However, with a plugin you will not display the related articles in content professional as the photo below? This article will guide you How to add “Related posts” in content of your WordPress blog:

add related posts in content

Step 1: Install and active the plugin Related Posts by Taxonomy

Step 2: Add the following code to the functions.php of your blog

// Code for count the number of paragraph
function count_paragraph( $insertion, $paragraph_id, $content ) {
        $closing_p = '</p>';
        $paragraphs = explode( $closing_p, $content );
        foreach ($paragraphs as $index => $paragraph) {
                if ( trim( $paragraph ) ) {
                        $paragraphs[$index] .= $closing_p;
                }
                if ( $paragraph_id == $index + 1 ) {
                        $paragraphs[$index] .= $insertion;
                }
        }
 
        return implode( '', $paragraphs );
}
// Code for insert related post in content
add_filter( 'the_content', 'prefix_insert_post_ads' );
 
function prefix_insert_post_ads( $content ) {
 
        $related_posts= do_shortcode('[related_posts_by_tax title=""]');
 
        if ( is_single() ) {
                return count_paragraph( $related_posts, 1, $content );
        }
 
        return $content;
}
Enjoy!

About - Hey, this blog belongs to me! I am the founder of TechTipLib and managing editor right now. And I love to hear what do you think about this article, leave comment below! Thank you so much...