2017-02-09 2 views
0

이 코드를 사용하는 방법은 "카테고리 별 관련 게시물"만 표시합니까? 이 코드는 "single.php"내에 있습니다 stackoverflow를하지 못했지만 해결책을 찾을 수 없습니다. 누구든지 나를 도울 수 있습니까?WordPress (taxonomy)의 관련 게시물 카테고리

당신의 allargw 배열 값
<?php 
    $postsPerPageq = 5; 

       $allargw = array(
       'post_type' => 'audioplayer', 
       'posts_per_page' => $postsPerPageq, 
       'orderby' => 'title', 
       'order' => 'DESC', 
      'tax_query' => array(
     array(
      'taxonomy' => 'audiotop', 
      'field' => 'slug', 
      'terms' => 'top-audio' 
     ) 
    ) 
      ); 
       $topaudio = new WP_Query($allargw); 
    while ($topaudio->have_posts()) : $topaudio->the_post(); 


    ?> 

<h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3> 


<?php endwhile; wp_reset_postdata();?> 
+1

체크 아웃 : - http://wordpress.stackexchange.com/a/41273 또는 http://wordpress.stackexchange.com/questions/30039/how-to-display-related-posts-from-same- 카테고리 –

답변

0

추가 카테고리 이름 ..

<?php 
    $postsPerPageq = 5; 

      $allargw = array(
      'post_type' => 'audioplayer', 
      'posts_per_page' => $postsPerPageq, 
      'category_name'=>'Your category name', 
      'orderby' => 'title', 
      'order' => 'DESC', 
     'tax_query' => array(
    array(
     'taxonomy' => 'audiotop', 
     'field' => 'slug', 
     'terms' => 'top-audio' 
    ) 
) 
     ); 
      $topaudio = new WP_Query($allargw); 
while ($topaudio->have_posts()) : $topaudio->the_post(); 


?> 

<h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3> 

희망이 당신에게 도움이!

업데이트 답 :이처럼 사용, 자동으로 카테고리를 추가하려면

..

wp_set_object_terms('your_post_id', 'your_category_name', 'your_taxonomy_name'); 

이 시도 할 수 있습니다 ..

+0

안녕하세요,하지만 카테고리를 자동으로 추가하는 방법은 무엇입니까? , 예 : 사용자가 카테고리 1 게시물에 액세스합니다. 그런 다음 '관련 게시물'은 카테고리 1 게시물 만 표시합니다. "카테고리 2, 카테고리 3 등등에도 동일하게 적용됩니다. –

+0

실제로 게시물을 삽입하고 카테고리를 자동으로 삽입 하시겠습니까? – Preethi

0

당신이 간단한 게시물 카테고리별로 게시물을 검색 할 경우 다음 코드를 확인하십시오 :

<?php 
    $category = get_the_category($post->ID); 
    $cat_id = $category[0]->term_id; 
    $postsPerPageq = 5; 
    $allargw = array(
       'post_type' => 'audioplayer', 
       'posts_per_page' => $postsPerPageq, 
       'orderby' => 'title', 
       'order' => 'DESC', 
       'cat' => $cat_id 

      ); 
    $topaudio = new WP_Query($allargw); 
    while ($topaudio->have_posts()) : $topaudio->the_post(); ?> 
     <h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3> 
    <?php endwhile; wp_reset_postdata();?> 

기타 검색하려는 경우 맞춤 분류로 게시물을 작성한 다음 아래 코드를 확인하십시오.

<?php 
    $term_list = wp_get_post_terms($post->ID, 'audiotop', array("fields" => "all")); 
    $term_id = $term_list[0]->term_id ; 
    $postsPerPageq = 5; 
    $allargw = array(
       'post_type' => 'audioplayer', 
       'posts_per_page' => $postsPerPageq, 
       'orderby' => 'title', 
       'order' => 'DESC', 
       'tax_query' => array(
      array(
       'taxonomy' => 'audiotop', 
       'field' => 'id', 
       'terms' => $term_id 
      ) 
     ) 
    ); 
    $topaudio = new WP_Query($allargw); 
    while ($topaudio->have_posts()) : $topaudio->the_post(); ?> 
     <h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3> 
    <?php endwhile; wp_reset_postdata();?> 

희망 사항.

+0

안녕하세요,하지만 카테고리를 자동으로 추가 하시겠습니까? 예 : 사용자가 카테고리에 액세스합니다. 1 게시물을 선택하면 "관련 게시물"에 범주 1 게시물 만 자동으로 표시됩니다. 범주 2, 범주 3 등에서도 마찬가지입니다. –

+0

한 가지를 명확히하십시오. 각 게시물은 하나의 범주에만 할당됩니다. post1-> category1, category2, post2-> category3, category1과 같이 post1-> category1, post2-> category2, post3-> category1, post4-> category-> 2가 아니라면 식별하기가 매우 어려울 것입니다 –

+0

각 게시물이 하나의 카테고리에 지정되면 내 업데이트 된 코드를 찾으십시오. –

1

첫째, 당신은 다음 조건이 WP_Query에 tax_query 위해 강타 잡아이 기능

get_the_terms (get_the_id(), 'your_custom_taxonomy'); 

을 사용하여 현재 포스트 용어를 잡아해야합니다.

$postsPerPageq = 5; 
$terms_slugs = array(); 

//get the current post terms slug 
$terms = get_the_terms(get_the_id(), 'audiotop'); 
foreach ($terms as $term) { 
    $terms_slugs[] = $term->slug; 
} 

//WP_Query args 
$allargw = array(
    'post_type' => 'audioplayer', 
    'posts_per_page' => $postsPerPageq, 
    'orderby' => 'title', 
    'order'  => 'DESC', 
    'tax_query' => array(
    array(
     'taxonomy' => 'audiotop', 
     'field' => 'slug', 
     'terms' => $terms_slugs, 
    ) 
    ) 
); 

나는 당신의 문제를 해결하기를 바랍니다.

관련 문제