WordPress.org

Ready to get started?Download WordPress

Forums

Exclude one category from category archive (2 posts)

  1. doser
    Member
    Posted 11 months ago #

    Hi,

    i'm working on a site for a client of mine that sells High end audio equipment. The site is setup so that each item can have categories added to them.

    The problem is that there is a category 'sold' that should only be shown on the sold page. Not on any other category page. Example when the user clicks on the 'Amplifier' category it should show all amplifiers except the sold ones. 'Sold' is also a category which can be added to an item.

    I've tried to exclude the category with a query post:

    <?php query_posts('cat=-14');?>

    The problem is that this will return all items, not just the amplifiers. I don't know houw to alter the loop and so that when you click the 'amplifier' link in navigation it will show the amplifiers minus the sold ones.

    Any help is very much appreciated because i'm going bonkers over this.

  2. keesiemeijer
    moderator
    Posted 11 months ago #

    Try querying from your theme's functions.php. Remove the query_post from your category template file and put this in your functions.php:

    function my_post_queries( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
        // a category page but not category 14 id
        if(is_category() && !is_category(14)){
          $query->set('cat', '-14');
        }
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    http://www.billerickson.net/customize-the-wordpress-query/

    btw:
    consider creating a child theme instead of editing your theme directly - if you upgrade the theme all your modifications will be lost.

Reply

You must log in to post.

About this Topic