2012-09-06 5 views
1

관련 게시물을 카테고리 페이지의 게시물 옆에 태그로 표시하고 싶습니다. 관련된 모든 게시물 코드는 찾을 수있는 single.php의 중첩 루프에서 사용해야하지만 카테고리 페이지의 루프에 있어야합니다.워드 프레스의 카테고리 템플릿 페이지에 태그로 관련 게시물 표시

그래서 당신은 카테고리 "고양이"에 갈 때 출력합니다 다음 "포스트 1 개 제목"카테고리 "고양이"태그 "고양이" "관련 포스트 1.1 제목"태그 "고양이" "관련 1.2 제목 "태그"고양이 "


"포스트 2 제목 "카테고리"고양이 "태그"TOMCATS " "관련 포스트 2.1 제목 "태그"TOMCATS " "관련 포스트 2.2 제목을 "게시 , 태그 "톰캣"


...

이것은 내가 생각해 낸 코드이지만 깨졌습니다.

`// 첫 번째 검색어 $ my_query = new WP_Query ('cat = 6');

// If first query have posts 
if($my_query->have_posts()) : 

    // While first query have posts 
    while ($my_query->have_posts()) : $my_query->the_post(); 
    ?> 

    <!-- start post --> 


    <!-- End post div --> 

     <?php 
     // tags 

      $tag_ids = array(); 
          foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id; 
          $args=array(
          'tag__in' => $tag_ids, 
          'post__not_in' => array($post->ID), 
          'posts_per_page'=>99, 
          'caller_get_posts'=>1 
          ); 
      // Second query 
      $my_second_query = new WP_Query('$args'); 

      // If second query have posts 
      if($my_second_query->have_posts()) : 
      ?> 

       <?php 
       // While second query have posts 
       while($my_second_query->have_posts()) : $my_second_query->the_post(); 


        ?> 
        <!-- start post --> 


    <!-- End post div --> 


        <?php 
       // End second while have posts 
       endwhile; 
       ?> 
    <?php 
    // End first while have posts 
    endwhile; 

// End if first query have posts 
endif; 
?>` 

이 경우에도 가능합니까? 나는 내 삶을 위해 모범을 찾을 수 없었다. 미리 감사드립니다.

답변

1

네, 가능합니다. 코드와 함께 올바른 방향을 향한 것처럼 보입니다. 현재 게시물의 태그를보고 다른 게시물을 찾는 사용자 정의 쿼리를 작성하기 만하면됩니다. 루프에있는 동안 single.php 또는 다른 곳에서 사용되는 것이 중요하지 않습니다. 당신의 functions.php 파일이 추가 :

function echo_related_posts() { 
    global $post; 
    // Get the current post's tags 
    $tags = wp_get_post_tags($post->ID); 
    $tagIDs = array(); 
    if ($tags) { 
     // Fill an array with the current post's tag ids 
     $tagcount = count($tags); 
     for ($i = 0; $i < $tagcount; $i++) { 
      $tagIDs[$i] = $tags[$i]->term_id; 
     } 
     // Query options, the magic is with 'tag__in' 
     $args = array(
      'tag__in' => $tagIDs, 
      'post__not_in' => array($post->ID), 
      'showposts'=> 5 
     ); 
     $my_query = new WP_Query($args); 
     // If we have related posts, show them 
     if ($my_query->have_posts()) { 
      $related = ''; 
      while ($my_query->have_posts()) { 
       $my_query->the_post(); 
       $current = $my_query->current_post + 1; 
       $related .= "Related post " . $current . ": "; 
       $related .= "<a href='" . get_permalink() . "' >"; 
       $related .= get_the_title(); 
       $related .= "</a>"; 
       if (($my_query->current_post + 1) != ($my_query->post_count)) $related .= ", "; 
      } 
      echo $related; 
     } 
     else echo "No related posts"; 
    } 
    else echo "No related posts"; 
    wp_reset_query(); 
} 

은 분명히 당신은 당신이 찾고있는 정확한 결과를 얻기 위해이 기능에 아무것도 변경할 수 있습니다, 그것은 다섯 관련 게시물까지 메아리 단지 예입니다. 사용자 지정 쿼리에 대한 추가 참조는 http://codex.wordpress.org/Class_Reference/WP_Query을 참조하십시오.

해당 기능을 사용하면 echo_related_posts() 함수에 액세스 할 수 있습니다.이 함수는 공통 태그로 관련된 게시물을 출력합니다. 그렇게하면 category.php 또는 중 템플릿, 당신이 뭔가를 할 수있는 카테고리 페이지에 사용되는 다음 (이 간결에 대한 과잉 단순화 루프, 단지 echo_related_posts() 기능주의) :

// Inside your existing loop 
<?php while (have_posts()) : the_post(); ?> 

    // Output the current post info here 

    // Output the related posts 
    <?php echo_related_posts(); ?> 

<?php endwhile; ?> 

은 관련 게시물을 가정 발견, 그것은 것입니다 출력이 같은 :

"관련 포스트 1 : 제목 하나, 관련 포스트 2 : 제목 두, 관련 포스트 3 : 제목 세"희망

당신이 거기까지 갈 수 있습니다!

+0

sooo 고맙습니다. 방금 휴가를 보냈고이 프로젝트를 계속 진행하고 결과를 알려 드리겠습니다. –

관련 문제