0

I have a question. I need to have, in my archive.php file, a exclusion of a specific category. given what I have now, how would I make that happen?

Here is the code:

    <?php 
        query_posts(array(
            'post_type' => 'post',
            'showposts' => 5
        ) );
    ?>
    <?php while (have_posts()) : the_post(); ?>
        <div class="blogcontentlinks">
            <a style="text-decoration:none;" href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a>
            <p style="">Posted on <?php the_time('m/j/y g:i A') ?><br />Categories: <?php the_category(' ') ?><br />Tags: <?php the_tags(' ') ?></p>
            <p><?php the_excerpt(); ?></p>
            <p>&nbsp;</p>
        </div>
    <?php endwhile;?>

Thank you

2 Answers 2

1

You can add category id or category slug into arguments you are passing to query_posts:

<?php
    // category slug ('products')
    query_posts(array(
        'post_type' => 'post',
        'showposts' => 5,
        'category_name' => 'products'
    ) );
?>

or

<?php
    // category id ('3')
    query_posts(array(
        'post_type' => 'post',
        'showposts' => 5,
        'cat' => '3'
    ) );
?>

In the second case you can use comma separated list of category ids like 'cat' => '3,5,7'.

1
  • Thank you. That worked perfectly. I'll select that as the answer as soon as wordpress.stackexchange allows me to Jan 10, 2013 at 14:31
1

Try this before the loop

<?php query_posts('cat=-[ID]'); ?>

The cat=-[ID] parameter. "-ID" in this case is the ID of the category, and you can replace that with any category you wish to remove from the loop. You may use comma separated values here to remove more than one category (e.g. cat=-1,-2 etc)

5
  • When category ID is negative you exclude this category.
    – Max Yudin
    Jan 10, 2013 at 14:28
  • 1
    The author is asking how to exclude a category. You can add @MikeD 's suggestion directly as a parameter: 'post_type' => 'post', 'showposts' => 5, 'cat' => '-6, -8'
    – isotrope
    Jan 10, 2013 at 14:31
  • @Max Yudin, i think that Nick Passaro need to exclude this category from archive.php and not include.
    – user24259
    Jan 10, 2013 at 14:31
  • isotrope, MikeD, you both are right. I was shamefully careless.
    – Max Yudin
    Jan 10, 2013 at 14:39
  • bah.. at least now, there's also code for showing only category X on the same question's page. You never know who might happen upon this question in six months :)
    – isotrope
    Jan 10, 2013 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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