Tui Học IT

Hướng Dẫn Tạo Danh Sách Bài Viết Liên Quan Cho Theme Flatsome

author
1 minute, 30 seconds Read

Hướng Dẫn Tạo Danh Sách Bài Viết Liên Quan Cho Theme Flatsome-natutool

Trong quá trình xây dựng website trên theme flatsome, vậy làm thế nào để tạo danh sách bài viết liên quan. Phần này khá là quạn trọng mà theme Flatsome lại không hỗ trợ. Nên này mình chia sẻ luôn.

Đầu tiên cần chèn đoạn code sau vào file functions.php của Child Theme.

/* Dev by Woovn */ add_shortcode('woovn_posts_related','flatsome_related_posts');
function flatsome_related_posts(){
ob_start();
$categories = get_the_category(get_the_ID());
if ($categories){
echo '<div class="relatedcat">';
$category_ids = array();
foreach($categories as $individual_category) array_push($category_ids, $individual_category->term_id);
$my_query = new wp_query(array(
'category__in' => $category_ids,
'post__not_in' => array(get_the_ID()),
'posts_per_page' => 6
));
$ids = wp_list_pluck( $my_query->posts, 'ID' );
$ids = implode(',', $ids);
if( $my_query->have_posts() ){
echo '<h3>Bài viết liên quan</h3>';
//echo do_shortcode('[blog_posts style="normal" columns="3" columns__md="2" ids="' . $ids . '" image_height="56.25%" text_align="left"]'); // Slider
echo do_shortcode('[blog_posts style="normal" type="row" columns="3" columns__md="2" posts="6" image_height="56.25%" text_align="left" ids="' . $ids . '"]'); // Row
}
echo '</div>';
}
return ob_get_clean();
}

Sau đó chúng ta chỉ việc chèn shortcode sau vào HTML after blog posts trong Tùy biến ▸ Blog ▸ Blog Single Post

[woovn_posts_related]

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *