引导手风琴错误地循环通过柱子

时间:2018-04-06 作者:Lucy Brown

我的网站上有此页面-https://visitwinona.com/ask-a-local/

在我调用自定义帖子类型FAQ的所有类别时,我实现了引导式手风琴,它可以工作,但当我添加一个新类别时,最后一个手风琴会打开一个随机类别,而不是它想要的类别。我看到它出于某种原因使用了相同的ID,但我不确定如何解决这个问题,

<?php
 /*
   * Loop through Categories and Display Posts within
 */

 $post_type = \'faqs\';

 // Get all the taxonomies for this post type
 $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 ) : ?>





        <?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,
                    )
                )

            );?>



            <h2 style="margin-bottom: 20px;">
              <a data-toggle="collapse" data-parent="#accordion" href="#collapse-<?php the_ID(); ?>" aria-expanded="true" aria-controls="collapse-<?php the_ID(); ?>">
                  <?php echo $term->name; ?> +
              </a>
            </h2>




         <div id="collapse-<?php the_ID(); ?>" class="panel-collapse collapse <?php if( $c == 1 ) echo \'in\'; ?>" role="tabpanel" aria-labelledby="heading-<?php the_ID(); ?>">


            <?php $localfaqs = new WP_Query($args);?>



           <?php if ( $localfaqs->have_posts() ) : while ( $localfaqs->have_posts() ) : $localfaqs->the_post(); $c++; ?>


           <div class="row">    
               <?php 

                   $faqs = get_field(\'local\');

                        if( $faqs ): ?>
                            <?php foreach( $faqs as $p ): // variable must NOT be called $post (IMPORTANT) ?>


                                   <div class="col-md-half faq-circle">
                                        <div class="celeb-circle-faq">
                                            <a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_post_thumbnail( $p->ID ); ?></a>
                                        </div>
                                    </div>

                            <?php endforeach; ?>
                        <?php endif; ?>



                <div class="col-md-10 faq-link">
                    <a href="<?php the_permalink();?>">
                        <?php the_title();?>
                    </a>
                </div>


           </div>


    <?php endwhile; endif; ?>

    </div>



<?php endforeach;

  endforeach; ?>
任何建议都很好,

谢谢

1 个回复
最合适的回答,由SO网友:Jacob Peattie 整理而成

你的问题是你正在使用the_ID() 为每个分类术语指定ID,但是the_ID() 不在foreach循环中输出当前项的ID,而是获取当前项的ID。

由于您没有在自定义查询中重置postdata,因此这将是上一学期最后一篇文章的ID。

要为每个手风琴指定唯一的ID,请将术语的ID与$term->term_id:

<?php
/*
 * Loop through Categories and Display Posts within
 */
$post_type = \'faqs\';

// Get all the taxonomies for this post type
$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 ) : 
        $args = array(
            \'post_type\'      => $post_type,
            \'posts_per_page\' => -1,  //show all posts
            \'tax_query\'      => array(
                array(
                    \'taxonomy\' => $taxonomy,
                    \'field\'    => \'slug\',
                    \'terms\'    => $term->slug,
                )
            )

        );
        ?>

        <h2 style="margin-bottom: 20px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapse-<?php echo $term->term_id; ?>" aria-expanded="true" aria-controls="collapse-<?php echo $term->term_id ?>">
                <?php echo $term->name; ?> +
            </a>
        </h2>

        <div id="collapse-<?php echo $term->term_id; ?>" class="panel-collapse collapse <?php if( $c == 1 ) echo \'in\'; ?>" role="tabpanel" aria-labelledby="heading-<?php echo $term->term_id; ?>">

            <?php $localfaqs = new WP_Query( $args );?>

            <?php if ( $localfaqs->have_posts() ) : while ( $localfaqs->have_posts() ) : $localfaqs->the_post(); $c++; ?>
                <div class="row">    
                    <?php 
                    $faqs = get_field(\'local\');

                    if ( $faqs ) : foreach ( $faqs as $p ): // variable must NOT be called $post (IMPORTANT) 
                        ?>

                        <div class="col-md-half faq-circle">
                            <div class="celeb-circle-faq">
                                <a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_post_thumbnail( $p->ID ); ?></a>
                            </div>
                        </div>

                    <?php endforeach; endif; ?>

                    <div class="col-md-10 faq-link">
                        <a href="<?php the_permalink();?>">
                            <?php the_title();?>
                        </a>
                    </div>
                </div>
            <?php endwhile; endif; wp_reset_postdata(); ?>
        </div>
    <?php
    endforeach;
endforeach;
我改变的是collapse-<?php the_ID(); ?>collapse-<?php echo $term->term_id ?>heading-<?php the_ID(); ?>heading-<?php echo $term->term_id; ?>.

我还补充道wp_reset_postdata(); 在自定义查询循环之后,这样循环之后的任何代码都不会引用错误的帖子。

结束

相关推荐

Loop inside query

我有一个数组来存储分类法和查询术语,但我不能使用foreach在tax\\u查询中循环我的数组。我拿到了505内线。 $query = array( \'post_type\' => $post_type, \'posts_per_page\' => -1, ); $query[\'tax_query\'] = array( \'relation\' => \'OR\