How to automatically insert a list of related articles between the content

There are many ways to show related articles, such as code generated related posts with tumbnails. Here, I will show you another way. It is automatically insert a list of related articles between the content

Please follow me:



// Code count the number of lines in text
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 );
}
//Insert a list of related articles between the 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;
}

The results are as follows:


Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
Posts RSSComments RSSBack to top
© 2011 Sharing the best tips for you and for me ∙ Designed by BlogThietKe
Released under Creative Commons 3.0 CC BY-NC 3.0