'; $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 ); } //Chèn bài liên quan vào giữa nội dung add_filter( 'the_content', 'prefix_insert_post_ads' ); function prefix_insert_post_ads( $content ) { global $post; $relate_post = '
'; wp_reset_query(); $terms = get_the_terms( $post_id, 'category' ); if ( empty( $terms ) ) $terms = array(); $term_list = wp_list_pluck( $terms, 'slug' ); $related_args = array( 'post_type' => 'post', 'posts_per_page' => '4', 'orderby' => 'rand', 'post_status' => 'publish', 'post__not_in' => array(get_the_ID()), 'tax_query' => array( array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => $term_list ) ) ); $related = new WP_Query( $related_args ); if( $related->have_posts() ) : $relate_post = '

Nên đọc bài sau:

'; endif; wp_reset_postdata(); wp_reset_query(); if ( is_single() ) { return count_paragraph( $relate_post, 2, $content ); } return $content; }