0

I have this query:

$cat_array = array();
    $args=array(
      'post_type' => 'post',
      'post_status' => 'publish',
      'exclude' => '187',
      'posts_per_page' => 19,
      'caller_get_posts'=> 1
      );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post();
        $cat_args=array('orderby' => 'none');
        $cats = wp_get_post_terms( $post->ID , 'category', $cat_args);
        foreach($cats as $cat) {
          $cat_array[$cat->term_id] = $cat->term_id;
        }
      endwhile;
    }
    if ($cat_array) {
      foreach($cat_array as $cat) {
        $category = get_term_by('ID',$cat, 'category');
        echo '<li><a href="' . esc_attr(get_term_link($category, 'category')) . '" title="' . sprintf( __( "View latest posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>'.'</li>';
      }
    }
    wp_reset_query();

Which is listing the latest updated categories. All good. But I want to exclude a particular category from listing over there. That can be done like I have in the query'exclude' => '187', BUT the problem is with the following example: I have a post which belongs to two categories, one category is the one I don't want to display, and the other one is a category I do want to display. If I simply exclude it like I said above, none of the categories will display in the list, because it will completely exclude that post. How can I still show the other category, and hide the one I don't want tho show?

Any clue?

thanks

1

Just skip the excluded category in your foreach:

foreach ( $cat_array as $cat ) {
    if ( $cat != EXCLUDED_CAT_ID ) {
        // Output
    }
}
  • thanks. I didn't get to try because I've already solved – whitelord May 15 '15 at 12:36
0

solved this with

 if ($cat_array) {
      foreach($cat_array as $cat) {
        $category = get_term_by('ID',$cat, 'category');
     if($category->name != 'Excluded category'){
        echo '<div class="catspace"><a href="' . esc_attr(get_term_link($category, 'category')) . '" title="' . sprintf( __( "latest posts %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>'.'</div>';
     }else{echo '<div class="catspace">'. ''   .'</div>';    }
     }
   }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.