0

我的双循环查询在这里有些不太正常,我不确定它到底是什么。基本上,我使用 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>
4

1 回答 1

0

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

<!-- 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-08-31T22:02:00.047 回答