Ask your WordPress questions! Pay money and get answers fast! (more info)

Displaying blog with excluded categories in archive problem

  • SOLVED

I am trying to add a blog to a site with the usual index, article, category and single views. I am using the post/category functionality for other things such as controlling sliders etc. I need to exclude these categories from the blog pages.

I am using Wordpress 3 which has most of the coding in the loop.php file. I have tried adding a plugin to exclude categories and it has partially worked along with putting

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

at the top of loop.php

However the archive page does not seem to work correctly on the archives section, it displays everything in the date archive.

Here is the site in question I have removed the plugin and rolled it back to the standard way things are and it is still not showing the archive properly.

Site : [[LINK href="http://tuff.gunkdesign.co.uk/news"]][[/LINK]]

What I need an answer on is how to display a blog section with categories and archive but with some categories excluded ?

Answers (6)

2010-12-21

Chris Bavota answers:

You can add a conditional tag on your archive.php file within the WordPress loop that ignores any post within that category. Or in the loop.php file if your theme is using it. Here is what it would look like in the appropriate place in the TwentyTen loop.php file.

<?php if(!in_category("6")) : ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>

<div class="entry-meta">
<?php twentyten_posted_on(); ?>
</div><!-- .entry-meta -->

<?php if ( is_archive() || is_search() ) : // Only display excerpts for archives and search. ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<?php endif; ?>

<div class="entry-utility">
<?php if ( count( get_the_category() ) ) : ?>
<span class="cat-links">
<?php printf( __( '<span class="%1$s">Posted in</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?>
</span>
<span class="meta-sep">|</span>
<?php endif; ?>
<?php
$tags_list = get_the_tag_list( '', ', ' );
if ( $tags_list ):
?>
<span class="tag-links">
<?php printf( __( '<span class="%1$s">Tagged</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); ?>
</span>
<span class="meta-sep">|</span>
<?php endif; ?>
<span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?></span>
<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="meta-sep">|</span> <span class="edit-link">', '</span>' ); ?>
</div><!-- .entry-utility -->
</div><!-- #post-## -->

<?php comments_template( '', true ); ?>
<?php endif; ?>


You can read more about the conditional tag here:

[[LINK href="http://codex.wordpress.org/Conditional_Tags#A_Category_Page"]][[/LINK]]

2010-12-21

Peter Michael answers:

Add a filter:


# Exclude categories | functions.php
function fd_remove_cat( $notused )
{
global $wp_query;
$wp_query->query_vars['cat'] = '-3';
}
add_action('pre_get_posts', 'fd_remove_cat' );


Also see [[LINK href="http://codex.wordpress.org/Custom_Queries"]]http://codex.wordpress.org/Custom_Queries[[/LINK]], section 'Category Exclusion'

HTH


gunkdesign comments:

After using and selecting you as a partial winner I came across a problem with your solution.

Although this solution works perfectly on the front end in the admin under "Posts" the categories are also excluded. This isn't practical for my end user as they cannot then edit their posts (i am using the excluded categories to control other things like banners) themselves.

Is there anything I can change in the function to make it work on the front end queries only?

Thanks

2010-12-21

Pippin Williamson answers:

Try this plugin: [[LINK href="http://wordpress.org/extend/plugins/advanced-category-excluder/"]]http://wordpress.org/extend/plugins/advanced-category-excluder/[[/LINK]]

2010-12-21

S├ębastien | French WordpressDesigner answers:

could you paste all your loop.php

2010-12-21

idt answers:

Have you also added <?php query_posts('cat=-6'); ?> to archive.php?

2010-12-22

Lucas Wynne answers:

To do this easy use this plugin! [[LINK href="http://wordpress.org/extend/plugins/simply-exclude/"]]http://wordpress.org/extend/plugins/simply-exclude/[[/LINK]]. It's very customizable and great for customizing those posts displayed.