Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

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'.

share|improve this answer
    
Thank you. That worked perfectly. I'll select that as the answer as soon as wordpress.stackexchange allows me to –  Nick Passaro Jan 10 '13 at 14:31
add comment

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)

share|improve this answer
    
When category ID is negative you exclude this category. –  Max Yudin Jan 10 '13 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 '13 at 14:31
    
@Max Yudin, i think that Nick Passaro need to exclude this category from archive.php and not include. –  MikeD Jan 10 '13 at 14:31
    
isotrope, MikeD, you both are right. I was shamefully careless. –  Max Yudin Jan 10 '13 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 '13 at 14:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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