WordPress.org

Ready to get started?Download WordPress

Forums

Best Way To Exclude Single Category In index.php of Theme (4 posts)

  1. tred
    Blocked
    Posted 1 year ago #

    What is a simple and effective way to exclude posts in a single category from displaying on the index.php?

    My while loop looks like

    <?php while (have_posts()) : the_post(); ?>
    
    <div class="post">
    <div id="post_title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent lenke til: <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
    
    <div class="date"><?php the_time(get_option('date_format')); ?></a> - Posted in: <?php the_category(' & '); ?></div>
    <?php the_content(''); ?>
    <div class="postmeta">
    <span class="left"><?php if (function_exists('the_tags')) the_tags('Tags: '); ?></span>
    <span class="right"><?php comments_popup_link('No Comments', '1 Comment', '% Comments', 'comments'); ?></span>
    </div>
    </div>
    
    <?php endwhile; ?>

    The category ID is 3

    [ Please do not bump, that's not permitted here. ]

  2. deepbevel
    Member
    Posted 1 year ago #

    try this

    <?php query_posts('cat=-3'); ?>
    <?php while (have_posts()) : the_post(); ?>
    
    <div class="post">
    <div id="post_title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent lenke til: <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
    
    <div class="date"><?php the_time(get_option('date_format')); ?></a> - Posted in: <?php the_category(' & '); ?></div>
    <?php the_content(''); ?>
    <div class="postmeta">
    <span class="left"><?php if (function_exists('the_tags')) the_tags('Tags: '); ?></span>
    <span class="right"><?php comments_popup_link('No Comments', '1 Comment', '% Comments', 'comments'); ?></span>
    </div>
    </div>
    
    <?php endwhile; ?>
  3. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    just adding a fixed query before the loop will lead to all sorts of problems, the least of them being that the pagination will not work.

    at least review and implement: http://codex.wordpress.org/Function_Reference/query_posts#Preserving_Existing_Query_Parameters

    a better way is to use a 'pre_get_posts' filter;

    for your problem, there is an example in the Codex:
    http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts#Example:_Exclude_categories_on_your_main_page

  4. deepbevel
    Member
    Posted 1 year ago #

    yes, good call. I might have at least added my own condtional..

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.