你的问题是你正在使用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();
在自定义查询循环之后,这样循环之后的任何代码都不会引用错误的帖子。