2011-10-09 3 views
0

좋아 나는이 루프 코드가 있습니다 : 현재 루프 카테고리 아이들이 다음 정말 H2의 같은 아이들 이름을 모두 (또는 무엇이든을 표시 할 경우 내가해야 할 일은WordPress의 경우

<?php 
    //get all categories then display all posts in each term 
    $taxonomy = 'category'; 
    $param_type = 'category__in'; 
    $term_args=array(
     'orderby' => 'name', 
     'order' => 'ASC', 
     'hide_empty' => 0, 
     'hierarchical' => 0 
    ); 
    $terms = get_terms($taxonomy,$term_args); 
    if ($terms) { 
     foreach($terms as $term) { 
     $args=array(
      "$param_type" => array($term->term_id), 
      'post_type' => 'products', 
      'post_status' => 'publish' 
     ); 
     $my_query = null; 
     $my_query = new WP_Query($args); 
     if(/*$my_query->have_posts()*/ 1==1) { ?> 
      <div id="<?php echo str_replace(" ","",$term->name); ?>" class="category section"> 
      <h3 class="categoryTitle"><?php echo $term->name;?></h3> 
      <?php 
      while ($my_query->have_posts()) : $my_query->the_post(); ?> 
      <div class="product"> 
       <h3 class="productTitle"><?php the_title(); ?></h3> 
       <div class="description"><?php the_content(); ?></div> 
      </div> 
      <div class="clearfix"></div> 
      <?php 
      endwhile; 
      ?> 
      </div> 
    <?php 
     } 
     } 
    } 
    wp_reset_query(); // Restore global post data stomped by the_post(). 
    ?>  

이며,)? 내가 어떻게 할 수 있니?

답변

1

나는 다음과 같이 코드를 교체하려고 줄은 :

if ($terms) : 

foreach($terms as $term) : 

$args=array(
    "$param_type" => array($term->term_id), 
    'post_type' => 'products', 
    'post_status' => 'publish' 
); 

$children = get_posts($args); 

if($children) : ?> 

<div id="<?php echo str_replace(" ","",$term->name); ?>" class="category section"> 
    <h3 class="categoryTitle"><?php echo $term->name;?></h3> 

    <?php foreach($children as $child) : setup_postdata($child); ?> 

      <div class="product"> 
       <h3 class="productTitle"><?php the_title(); ?></h3> 
       <div class="description"><?php the_content(); ?></div> 
      </div> 

    <?php endforeach; ?> 

</div> 


<?php endif; // if($children) 

endforeach; 

endif; // if($terms) 

나는 이것을 테스트하지했지만 그것은 올바른 방향에 투입한다!

는 도움이되기를 바랍니다.

+0

건배 Dave는 광범위한 노력을 기울이고 있습니다. – benhowdle89

0

나는 똑같은 문제가 있었지만, 나는 현재 게시물 카테고리가 부모인지 아닌지를 확인함으로써 간단한 해결책을 찾았다 고 생각한다.

$term = get_queried_object(); 


if($term->post_parent != 0){ 
    echo 'has parent'; //this post category has child 
}else{ 
    echo 'no parent'; //this post category doesn't have child 
} 

이 정보가 도움이되기를 바랍니다.