修复重复检查逻辑:
重复检查逻辑错误。您需要将这些术语保存在一个数组中,然后检查该术语是否已经存在于数组中,以确定它是否重复。
因此if ( $dupe == 0 ) :
条件和错误的循环嵌套,如下所示:
if ( have_posts() ) :
echo \'<ul class="filter">\';
echo \'<li class="current-menu-item"><a href="#" data-filter="*">\' . __( \'All\' ) . \'</a></li>\';
$unique_terms = array(); // instead of $dupe = 0;
while ( have_posts() ) : the_post();
$term_list = wp_get_post_terms( $post->ID, \'media_blog_category\', array( \'fields\' => \'all\' ) );
foreach( $term_list as $term ) :
if( ! in_array( $term->term_id, $unique_terms ) ):
array_push( $unique_terms, $term->term_id );
echo \'<li><a href="#" data-filter=".media_blog_category-\' . $term->slug . \'">\' . $term->name . \'</a></li>\';
endif;
endforeach; //term_list
endwhile;
echo \'</ul>\';
endif; //WP_Query
这应该是你想要的。
更好的解决方案:
在使用上述逻辑进行修复后,代码将正常工作。但是,您可以使用以下代码提高效率:
if ( is_post_type_archive( \'media_blog\' ) ) :
$args = array(
\'post_type\' => \'media_blog\',
\'posts_per_page\' => 16, // same as the pre_get_posts
\'post_status\' => \'publish\',
\'paged\' => get_query_var( \'paged\' ),
\'fields\' => \'ids\' // for this specific case, all you need are the IDs
);
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
echo \'<ul class="filter">\';
echo \'<li class="current-menu-item"><a href="#" data-filter="*">\' . __( \'All\' ) . \'</a></li>\';
$term_list = wp_get_object_terms( $wp_query->get_posts(), \'media_blog_category\' );
foreach( $term_list as $term ) :
echo \'<li><a href="#" data-filter=".media_blog_category-\' . $term->slug . \'">\' . $term->name . \'</a></li>\';
endforeach; //term_list
echo \'</ul>\';
endif; //WP_Query
wp_reset_query();
endif; //media_blog