首页 文章

获取具有重复计数的自定义自定义分类术语

提问于
浏览
0

我有自定义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 回答

  • 0

    这是你可以尝试的东西......

    $count_terms = array_count_values($all_terms);
    $count_slugs = array_count_values($all_slugs);
    
  • 0

    你可以这样做,稍微缩短一点 .

    $all_terms = array();
    $all_slugs = array();
    foreach ( $myMapPosts as $post ) : setup_postdata( $post );
        $terms = wp_get_post_terms( $post->ID, array( 'listing_category' ) );    
        foreach ( $terms as $term ) {        
            $all_terms[$term->slug] = array('name' => $term->name, 'slug' => $term->slug, 'count'=>0); // Used for Display Purposes
            $all_slugs[] = $term->slug;
        } 
    endforeach;
    
    $terms = array_count_values($all_slugs);
    foreach ($terms as $key => $value) {
        $all_terms[$key]['count'] = $value;
    }
    
    var_dump($all_terms);
    

    这样您就不必担心删除重复项了

    结果

    array(3) {
      ["restaurants-cafes"]=>
      array(3) {
        ["name"]=>
        string(23) "Restaurants & Cafes"
        ["slug"]=>
        string(17) "restaurants-cafes"
        ["count"]=>
        int(20)
      }
      ["food-stores-markets"]=>
      array(3) {
        ["name"]=>
        string(25) "Food Stores & Markets"
        ["slug"]=>
        string(19) "food-stores-markets"
        ["count"]=>
        int(8)
      }
      ["other"]=>
      array(3) {
        ["name"]=>
        string(5) "Other"
        ["slug"]=>
        string(5) "other"
        ["count"]=>
        int(2)
      }
    }
    

    希望这有帮助!

相关问题