我倾向于这样做:
$terms = array();
$terms_tags = wp_get_post_terms( $query->post->ID, array( \'post_tag\' ) );
foreach ( $terms_tags as $term_tag ) {
$terms[$term_tag->slug] = \'<button class="filter" data-filter=".\'.$term_tag->slug.\',">\'.$term_tag->name.\'</button>\';
}
echo implode($terms,\', \');
它利用了PHP不允许重复的数组键这一事实,从而避免了函数调用的开销。
在您的情况下,它应该是这样的(没有所有的PHP标记垃圾邮件):
$query = new WP_Query( $args ); ?>
<div class="entry-content units-row group">
<div class="category-list category-list-video-archive">
Filter by tags:
<button class="filter" data-filter="all">Show All</button><?php
$terms = array();
while ( $query->have_posts() ) :
$query->the_post();
$terms_tags = wp_get_post_terms( $query->post->ID, array( \'video-arc-tags-taxonomy\' ) );
foreach ( $terms_tags as $term_tag ) {
$terms[$term_tag->slug] = \'<button class="filter" data-filter=".\'.$term_tag->slug.\'">\'.$term_tag->name.\'</button>\';
}
endwhile;
echo implode($terms,\' \'); // this goes outside the while loop ?>
</div><!-- .category-list -->
</div><!-- .entry-content -->