我有自定义wordpress查询自定义帖子类型与自定义分类 . 我试图回应一个链接的术语(标签)列表 . 对于重复的术语,我想显示重复术语的数量 . 不幸的是,在调用array_unique()之前,我无法弄清楚如何计算项目并存储信息 . 任何建议将不胜感激 . 这是我的Wordpress查询:
//Begin Custom Query
$args=array(
'post_type' => 'gw_activity',
'post_status' => 'publish',
'orderby' => 'date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'activity_category',
'value' => 'mindful_life',
'compare' => '='
)
),
'posts_per_page' => -1
);
$my_query = null;
$my_query = new WP_Query($args);
这是我如何构建我的术语数组:
if( $my_query->have_posts() ) {
$all_terms = array();
$all_slugs = array();
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $terms = wp_get_post_terms( $my_query->post->ID, array( 'gw_activity_tag' ) ); ?>
<?php
foreach ( $terms as $term ) {
$all_terms[] = $term->name; // Used for Display Purposes
$all_slugs[] = $term->slug; // Used in the Final Permalink of the term
}
?>
<?php endwhile; }
wp_reset_query();
// End Custom Query
以下是我如何制作独特的术语和段塞数组:
$unique_terms = array_unique( $all_terms ); // Eliminate Duplicate Terms
$unique_slugs = array_unique( $all_slugs ); // Eliminate Duplicate Slugs
$final_term_list = array(); //Build Array of Unique Terms
$i = 0;
foreach ($unique_terms as $value) { // Build a Unique Array of Terms
$final_term_list[$i] = $value;
$i++;
}
$final_slug_list = array(); //Build Array of Unique Slugs
$s = 0;
foreach ($unique_slugs as $slug_value) { // Build a Unique Array of Slugs
$final_slug_list[$s] = $slug_value;
$s++;
}
这是我输出数据的方式
$fl = 0; //Create a counter to index the array
foreach($final_slug_list as $final_slug) {
echo '<a href="'. get_bloginfo('url') .'/activity-category/'. $final_slug_list[$fl] .'/">'.$final_term_list[$fl].'</a>';
$fl++;
} // End foreach
2 回答
这是你可以尝试的东西......
你可以这样做,稍微缩短一点 .
这样您就不必担心删除重复项了
结果
希望这有帮助!