【问题标题】:Using ACF to generate taxonomy bootstrap tabs使用 ACF 生成分类引导选项卡
【发布时间】:2016-08-31 20:10:15
【问题描述】:

我的双循环查询在这里有些问题,我不确定它到底是什么。基本上,我使用 ACF 在我的博客页面上提供了一个中继器字段,我可以在其中选择要在我的选项卡中显示的类别/分类法。因此,它可以很好地提取选项卡的名称,但我似乎无法在那里生成任何内容。有人建议我需要添加“reset_rows();”,我这样做了,但这似乎使页面需要永远加载,并且仍然没有创建任何实际内容。

这是我的循环。任何可以提供的帮助将不胜感激。

<!-- 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>

【问题讨论】:

    标签: wordpress advanced-custom-fields bootstrap-tabs


    【解决方案1】:

    好吧,我一直在努力解决这个问题,并请其他程序员朋友帮助解决这个问题。我们只是用 foreach 而不是 while 重写了 tab 内容区域,现在它似乎工作得很好:

    <!-- 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>
    

    所以,如果其他人有兴趣,这就是它最终完成的方式。

    【讨论】:

      猜你喜欢
      • 2016-02-07
      • 1970-01-01
      • 2020-09-07
      • 2021-05-24
      • 2018-01-05
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 2016-06-17
      相关资源
      最近更新 更多