如何将is active类添加到第一个<li>
是否由查询生成?谢谢你,我很感激。
<?php
$_terms = get_terms( array(\'claim-accordion-type\') );
foreach ($_terms as $term) :
$term_slug = $term->slug;
$_posts = new WP_Query( array(
\'post_type\' => \'claims_accordion\',
\'posts_per_page\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'claim-accordion-type\',
\'field\' => \'slug\',
\'terms\' => $term_slug,
),
),
));
if( $_posts->have_posts() ) :
echo\'<li class="accordion-item " data-accordion-item>\';
echo\'<a href="#" class="accordion-title">\';
echo \'\'. $term->name .\'\';
echo\'</a>\';
while ( $_posts->have_posts() ) : $_posts->the_post();
?>
<div class="accordion-content" data-tab-content>
<a data-open="exampleModal1">
<h4><?php the_title(); ?></h4></a>
</div>
<div class="reveal" id="exampleModal1" data-reveal>
<?php the_content(); ?>
<button class="close-button" data-close aria-label="Close modal" type="button">
<span aria-hidden="true">×</span>
</button>
</div>
<?php
endwhile;
echo \'</li>\';
endif;
wp_reset_postdata();
endforeach;
?>
</ul>
最合适的回答,由SO网友:Ahmed Fouad 整理而成
<?php
$_terms = get_terms( array(\'claim-accordion-type\') );
$i = 0;
foreach ($_terms as $term) :
$term_slug = $term->slug;
$_posts = new WP_Query( array( \'post_type\' => \'claims_accordion\', \'posts_per_page\' => -1, \'tax_query\' => array( array( \'taxonomy\' => \'claim-accordion-type\', \'field\' => \'slug\', \'terms\' => $term_slug, ), ), ));
if( $_posts->have_posts() ) :
$i++;
if ( $i == 1 ) {
$class = \'is-active\';
} else {
$class = null;
}
echo\'<li class="accordion-item \' . $class . \'" data-accordion-item>\';
echo\'<a href="#" class="accordion-title">\';
echo \'\'. $term->name .\'\';
echo\'</a>\';
while ( $_posts->have_posts() ) :
$_posts->the_post();
?>
<div class="accordion-content" data-tab-content>
<a data-open="exampleModal1">
<h4><?php the_title(); ?></h4></a>
</div>
<div class="reveal" id="exampleModal1" data-reveal>
<?php the_content(); ?>
<button class="close-button" data-close aria-label="Close modal" type="button">
<span aria-hidden="true">×</span>
</button>
</div>
<?php
endwhile;
echo \'</li>\';
endif;
wp_reset_postdata();
endforeach;
?>
</ul>
?>