Hiển thị bài viết liên quan cùng custom taxonomy trong custom post type

Post Type
/*
 * Code hiển thị bài viết liên quan cho custom post type trong cùng 1 custom taxonomy
 */
$postType = 'post-type';
$taxonomyName = 'category_post_type';
$taxonomy = get_the_terms(get_the_ID(), $taxonomyName);
if ($taxonomy){
   echo '<div class="relatedcat">';
   $category_ids = array();
   foreach($taxonomy as $individual_category) $category_ids[] = $individual_category->term_id;
   $args = array( 
      'post_type' =>  $postType,
      'post__not_in' => array(get_the_ID()),
      'posts_per_page' => 3,
      'tax_query' => array(
         array(
            'taxonomy' => $taxonomyName,
            'field'    => 'term_id',
            'terms'    => $category_ids,
         ),
      )
   );
   $my_query = new wp_query($args);
   if( $my_query->have_posts() ):
      echo '<p>Bài viết liên quan:</p><ul>';
      while ($my_query->have_posts()):$my_query->the_post();
         echo '<li><a href="'.get_the_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a></li>';
      endwhile;
      echo '</ul>';
   endif; wp_reset_query();
   echo '</div>';
}

Trong đoạn code ví dụ trên ta có:

  • Custom post type có tên là post-type
  • Custom taxonomy là category_post_type

Để hiển thị bài viết liên quan bạn hãy đặt đoạn code này vào chỗ bạn muốn hiển thị trong single-post-type.php nha.
Chúc các bạn làm tốt!

Related Post

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

0336931028
Facebook Skype Youtube Chat FB