2010-05-29 6 views
1

하위 범주 게시물의 표시를 방지하는 방법을 알고 싶습니다. 내 홈 페이지는 3 개의 "주요 카테고리"(상위 카테고리)의 모든 게시물을 나열하지만 불행히도 하위 카테고리의 게시물도 나열합니다.WordPress : 하위 범주 게시 방지 표시

<h2>Category Name</h2> 
<ul> 
    <?php $category_query = new WP_Query(array('category_name' => 'category1', 'showposts' => 5)); ?> 
    <?php while ($profissionais_query->have_posts()) : $profissionais_query->the_post(); ?> 
    <li> 
     <a class="title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
     <?php the_excerpt(); ?> 
    </li> 
    <?php endwhile; ?> 
</ul> 

누구 아이디어가 있습니까 :

여기에 내가 특정 카테고리에서 게시물을 얻기 위해 사용하고 코드인가?

감사합니다.

답변

1

새 쿼리 스타일을 시도해보십시오. 하나의 카테고리 만 보여줍니다.

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

를 추가하여 달리 실행 한 후 사용자 지정 쿼리를 삭제하면 페이지에서 다른 쿼리가 실행될 수 있습니다. 끝내기 직전에 추가하십시오. – Jared

+0

songdogtech : 불행히도이 코드는 여전히 하위 카테고리 게시물을 표시하기 때문에 작동하지 않았습니다. 또 다른 아이디어가 있습니까? 고맙습니다. –

+0

카를로스; 나는 그것을 하위 범주와 함께 테스트했으며 하위 범주는 보여주지 않았다; 하위 카테고리에는 고유 한 카테고리 ID가 있으므로 특별히 호출해야합니다. 다른 루프 나 코드가없는 페이지 템플릿 (표준 WP 루프 제외)에서 충돌을 분리 해보십시오. Jared : 자체 포함 된 쿼리이기 때문에 wp_reset_query가 필요하지 않습니다. 여러 사이트, 페이지/게시물에 여러 번 사용하고 쿼리 루프 충돌을 전혀 사용하지 않습니다. – markratledge

1

이 작동합니다 : 그것은 페이지 또는 게시물에 mutliple 번 사용할 수 있습니다 충돌없이 (PHP의 실행과 사용)

<?php $category_ID = $cat; // get ID of current category ?> 

<?php $excludes = get_categories('child_of='.$category_ID) ; 

    // For each child, add just the ID to an array 
    foreach ($excludes as $key => $value){ 
     $exs[] = $value->cat_ID; 
    } 

$my_query = new WP_Query(array(
      'cat' => $category_ID, 
      'category__not_in' => $exs 

)); 
if ($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post(); 
?> 
0

코드는 현재 카테고리 게시물을 표시합니다 아래

<?php 
$current_cat = get_query_var('cat'); 

$args=array(
    'category__in' => array($current_cat), 
    'showposts' => 5 
); 

query_posts($args); 

set_query_var("cat",$current_cat); 

if (have_posts()) : 

    while (have_posts()) : the_post(); 
?> 
     <a class="title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
     <?php the_excerpt(); ?> 
<?php 

    endwhile; 

else : 

?> 
     <h2>Nothing found</h2> 
<?php 

endif; 

?>