WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Excluding a category (3 posts)

  1. haussamen
    Member
    Posted 1 year ago #

    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!

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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));
  3. haussamen
    Member
    Posted 1 year ago #

    The first worked. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic