2010-05-03 3 views
0

을 기반으로 워드 프레스 글을 보여줍니다. 하지만 내가 찾고있는 것은 다음과 같다. 먼저 현재 게시물 (퍼머 링크를 기반으로 표시되는 게시물)이 속한 범주를 확인한 다음 동일한 범주에 속한 최신 게시물 만 나열합니다. 무엇을 편집해야합니까? 감사!선택적으로 현재 내가 (코드가 잘 작동) 워드 프레스를위한 사이드 바 코드의 일환으로 다음 코드를 사용하고 카테고리

답변

0

http://codex.wordpress.org/Function_Reference

당신이 get_the_category을 원하는거야처럼 소리()를 살펴 보자. 게시물은 여러 카테고리에 속할 수 있습니다. 그런 다음 카테고리 플래그를 전달하는 get_posts()를 호출하고 원하는 것이 무엇이든하려고합니다.

0

이것은 가장 짧고 똑똑한 해결책은 아니지만 오타가 포함되지 않은 경우이 방법으로 작동해야합니다.

게시물에는 여러 카테고리가있을 수 있으며 아래 스크립트는 첫 번째 카테고리에만 관심이 있습니다. 하지만 스크립트를 수정하고 여러 카테고리의 최근 게시물을 선택할 수도 있습니다.

<?php 
if (is_single()) : 
    $post_id = $wp_query->posts[0]->ID; // get id of post 
    $cats_of_post = get_the_category($post_id); // get categories by post id 
    $first_cat_id = $cats_of_post[0]->cat_id; // get first category id 
    $first_cat_name = $cats_of_post[0]->cat_name; // get category name 
?> 
<div id="widget-container-recent-in-category"> 
    <div class="widget-title"> 
     <h3>Latest posts in <?php echo $first_cat_name; ?>:</h3> 
    </div> 
    <div class="widget-content"> 
     <ul> 
     <?php 
      global $post; 
      $posts_in_cat = get_posts('numberposts=5&category='.$first_cat_id); 
      // iterate over posts in category and output as listitem 
      foreach($posts_in_cat as $post) : 
     ?> 
      <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
     <?php 
      endforeach; 
     ?> 
     </ul> 
    </div> 
</div> 
<?php 
    endif; 
?> 
+0

덕분에, 와우 확실히 뭔가 ... 이 솔루션을 테스트하고 내 코드에 맞는 방법을 확인하는 시간을해야합니다. – theDoctor

0

이 게시물 또는 페이지에 mutliple 번 사용할 수있는 새로운 쿼리는 (PHP는 간부와 함께 사용) 또는 사이드 서로 또는 주 WP 루프와 충돌하지 않습니다. 자신의 카테고리 이름 변경 mycategory :

<?php $my_query = new WP_Query('category_name=mycategory&showposts=12'); ?> 
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?> 
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"> 
<?php the_title(); ?></a> 
<?php endwhile; ?> 
+0

고마워, 내가 다시 볼 시간이 있으면 시험해 볼거야. btw, 현재 코드가 주 루프와 충돌하지 않습니다. – theDoctor

관련 문제