WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Excluding a category

[Resolved] Excluding a category

  • I need to exclude a category (top stories, category 7) from showing up in the recent posts section below articles on my site. Here’s the code that brings in recent posts:

    <?php
    $category = get_the_category();
    $categories = array();
    foreach($category as $x){ array_push($categories, $x->cat_ID); }
    $categories_join =  implode(',',$categories);
    $exclude_post_id = $post->ID;
    $args = array('cat' => $categories_join, 'showposts' => $post_num, 'post__not_in' => array($exclude_post_id));
    ?>

    The category ID I want to exclude is 7. I can’t seem to make it work. Help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • only relevant edited lines shown;

    try:

    foreach($category as $x){ if( $x->cat_ID != 7 ) array_push($categories, $x->cat_ID); }

    or:

    $args = array('cat' => $categories_join, 'showposts' => $post_num, 'post__not_in' => array($exclude_post_id), 'category__not_in' => array(7));

    The first worked. Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] Excluding a category’ is closed to new replies.
Skip to toolbar