2016-08-31 38 views
0

這裏我的雙循環查詢有些問題並不完全正確,我不確定它究竟是什麼。基本上,我使用ACF在我的博客頁面上提供了一個Repeater字段,我可以選擇在我的選項卡中顯示哪些類別/分類。因此,在標籤的名稱上做得很好,但我似乎無法在那裏生成任何內容。有人建議我需要添加「reset_rows();」,但這似乎是讓頁面永久加載,並且仍然不會創建任何實際內容。使用ACF生成分類標準引導標籤

這是我的循環。任何可以提供的幫助將不勝感激。

<!-- Nav Tabs --> 
<ul class="nav nav-pills"> 
    <?php if (have_rows('home_categories')) { 
     $i = 0; 
     while (have_rows('home_categories')) { 
      the_row(); 
      $term = get_sub_field('categories'); ?> 
    <li class="<?php if ($i == 0) { echo ' active'; }?>"> 
     <a href="#<?php echo $term->name; ?>" data-toggle="tab"><?php echo $term->name; ?></a> 
    </li> 
     <?php $i++; 
     } 
    } ?> 
</ul> 

<!-- Tab Content --> 
<div class="tab-content clearfix"> 
    <?php if (have_rows('home_categories')) { 
     $i = 0; 
     while (have_rows('home_categories')) { 
      the_row(); 

      $args = array(
       'post_type' => 'post', 
       'tax_query' => array(
        array(
         'taxonomy' => $term->taxonomy, 
         'terms' => array($term_id) 
        ) 
       ), 
       'posts_per_page' => 5, 
       'orderby' => 'date', 
       'order' => 'ASC', 
      ); 
      $post = new WP_Query($args); 
      while($post->have_posts()) : $post->the_post(); 

      $term = get_sub_field('categories'); ?> 

       <div class="tab-pane fade<?php if ($i == 0) { echo ' in active'; }?>" id="<?php echo $term->name; ?>"> 
        <?php the_post_thumbnail('thumbnail', array('class' => 'img-responsive img-thumbnail')); ?> 
        <a href="<?php echo get_permalink(); ?>"><h3><?php the_title(); ?></h3></a> 
        <?php the_excerpt(); ?> 
       </div> 
      <?php endwhile; 
     } 
     $i++; 
    } 
     wp_reset_postdata(); ?> 
</div> 

回答

0

那麼,我一直在這個工作,並要求其他程序員朋友幫忙找出答案。我們只是改寫了標籤內容區域在foreach,而不是一段時間,現在看來工作得很好:

<!-- Nav Tabs --> 
<ul class="nav nav-pills"> 
    <?php if (have_rows('home_categories')) { 
     $i = 0; 
     while (have_rows('home_categories')) { 
      the_row(); 
      $term = get_sub_field('categories'); ?> 
    <li class="<?php if ($i == 0) { echo ' active'; }?>"> 
     <a href="#tab-pane-<?php echo $i; ?>" data-toggle="tab"><?php echo $term->name; ?></a> 
    </li> 
     <?php $i++; 
     } 
    } ?> 
</ul> 

<!-- Tab Content --> 
<div class="tab-content clearfix"> 
    <?php 

     $home_categories = get_field("home_categories"); 
    // print_r($home_categories); 

     foreach($home_categories as $key => $home_cat) { 


      $term_id = $home_cat['categories']->term_id; 
      $term_name = $home_cat['categories']->name; 

      ?> 
      <div class="tab-pane fade<?php if ($key == 0) { echo ' in active'; }?>" id="tab-pane-<?php echo $key; ?>"> 
      <?php 


      $args = array(
       'post_type' => 'post', 
       'tax_query' => array(
        array(
         'taxonomy' => $term->taxonomy, 
         'terms' => array($term_id) 
        ) 
       ), 
       'posts_per_page' => 5, 
       'orderby' => 'date', 
       'order' => 'ASC', 
      ); 
      $query = new WP_Query($args); 

      foreach($query->posts as $post) { 

       ?> 

        <a href="<?php echo get_permalink($post->ID); ?>"><?php echo get_the_post_thumbnail($post->ID, 'thumbnail', array('class' => 'img-responsive img-thumbnail')); ?></a> 
        <a href="<?php echo get_permalink($post->ID); ?>"><h3><?php echo get_the_title($post->ID); ?></h3></a> 
        <?php the_excerpt(); ?> 
       <?php 


      } 

      ?> 
      </div> 
      <?php 



     }   ?> 

</div> 

因此,如果任何人有興趣,這是它是怎麼實現的最終。