2015-10-14 195 views
0

我必須回顯所有類別的帖子,這些帖子位於我創建的自定義帖子類型中。我目前有三個類別,我需要它,因爲我可能會添加另一個類別,這就是爲什麼我不必潛入編碼顯示另一個查詢。獲取自定義帖子類型的子類別

風格:

- Category title 
-- category post title, content etc. 

- Category2 title 
-- Category2 post contents 

我當前的代碼看起來像

 <?php 
     $args=array(
      'post_type' => 'edasimja' 
     ); 
     $my_query = null; 
     $my_query = new WP_Query($args); 
     if($my_query->have_posts()) { 
      while ($my_query->have_posts()) : $my_query->the_post(); ?> 

     <div class="col-sm-4"> 
      <?php $c = get_the_category(); ?> 
      <?php echo $c[0]->cat_name; ?> 
      <h2><?php the_title(); ?></h2> 
      <?php the_field('eli'); ?> 
     </div> 

      <?php 
      endwhile; 
     } 
     wp_reset_query(); 
     ?> 

此代碼分別querys每一個崗位,其貓的名字,但我需要每一個類別的列。我似乎無法做到這一點。

有人能指點我正確的方向嗎?

+0

你能告訴我們這段代碼是怎麼產生的嗎? – mdarmanin

+0

我的代碼爲每個帖子生成塊併爲其添加貓名。因此,如果我在貓「刀」內有兩個帖子,那麼它會將這個貓名稱「刀」添加到兩個帖子中,而不是將它們組合到一個塊中。 – StackSurfer

回答

1

所以,我找到了解決方案。

<?php 

    $post_type = 'features'; 
    $taxonomies = get_object_taxonomies(array('post_type' => $post_type)); 

    foreach($taxonomies as $taxonomy) : 

// Gets every "category" (term) in this taxonomy to get the respective posts 
$terms = get_terms($taxonomy); 

foreach($terms as $term) : ?> 

    <section class="category-section"> 

    <div class="row"> 
    <div class="span12"> 
     <h2 class="mid-heading"><?php echo $term->name; ?></h2> 
    </div> 

    <?php 
    $args = array(
      'post_type' => $post_type, 
      'posts_per_page' => -1, //show all posts 
      'tax_query' => array(
       array(
        'taxonomy' => $taxonomy, 
        'field' => 'slug', 
        'terms' => $term->slug, 
       ) 
      ) 

     ); 
    $posts = new WP_Query($args); 

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

     <div class="span4"> 

      <article class="inner-post clearfix"> 

       <div class="inner-img whitebox"> 
       <?php if(has_post_thumbnail()) { ?> 
         <?php the_post_thumbnail(); ?> 
       <?php } 
       /* no post image so show default */ 
       else { ?> 
         <img src="<?php bloginfo('template_url'); ?>/assets/img/default-img.png" alt="<?php echo get_the_title(); ?>" title="<?php echo get_the_title(); ?>" width="110" height="110" /> 
       <?php } ?> 
       </div> 

       <div class="inner-content"> 

       <h3 class="heading-size-14 font-weight-600"><a href="<?php echo get_permalink(); ?>" title="Read more about <?php echo get_the_title(); ?>"><?php echo get_the_title(); ?></a></h3> 

        <?php the_excerpt(); ?> 
       </div> 
      </article><!-- about-box --> 


     </div> 

    <?php endwhile; endif; ?> 
    </div> 
    <hr> 
    </section> 

<?php endforeach; 

endforeach; ?> 
相關問題