2 years ago
bibiyeu
in English ยท 284 Views
likes 4clips 1comments 0
How to auto 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 auto insert a list of related articles between the content
Please follow me:

Download and active plugin Related Posts by Taxonomy
Insert the code below into the file functions.php :
// 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:

bibiyeu clipped in 1 collections
0 comments