Thứ Tư, 14 tháng 12, 2016

Chèn bài viết liên quan giữa bài viết

Đầu tiên cài plugin: https://wordpress.org/plugins/related-posts-by-taxonomy/

Sau đó chèn đoạn code này vào file function.php



function count_paragraph( $namhp_related, $paragraph_related_namhp, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_related_namhp == $index + 1 ) {
$paragraphs[$index] .= $namhp_related;
}
}
return implode( '', $paragraphs );
}



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;
}

Không có nhận xét nào:

Đăng nhận xét