WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] exclude categories from homepage (5 posts)

  1. DrGizmondo
    Member
    Posted 4 years ago #

    Hi all I want to exclude three categories form my homepage, I am using the following code:

    <?php get_header(); ?>
    
    	<div id="content">
    
    <div id="feat_section">
    
    	<?php if (have_posts()) : ?>
    
    <!---FEATURE LOOP STARTS--->
    
    <?php query_posts('cat=5&showposts=1');
    $ids = array();
    while (have_posts()) : the_post();
    $ids[] = get_the_ID(); ?>
    
      <div class="feat_title">
    
                            <div class="feat_title">
    			<div class="post" id="post-<?php the_ID(); ?>">
    				<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    
                              </div>
    
    <div class="post_author">
    <p>By: <?php the_author_posts_link(); ?> | <?php the_date(); ?> | <?php the_time(); ?></p>
    
    </div>
    
    				<div class="entry">
    
    <?php if( get_post_meta($post->ID, "thumb", true) ): ?>
    <div class="homethumb">
    <img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&h=350&w=624&zc=0" alt="<?php the_title(); ?>" width="624" height="350" />
    </div>
    <?php else: ?>
    <?php endif; ?>
    
    				</div>
    
    			</div>
    
    <?php 
    
    endwhile;
    ?>
    
    </div>
    
    </div>
    
    <!---FEATURE LOOP END--->
    
    <!---NEWS - LEFT COLUMN LOOP START--->
    
    <div id="news_section">
    
    	<div id="column_01">
    
    <?php query_posts('showposts=15'); ?>
    <?php $posts = get_posts('numberposts=15&offset=0'); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count2 = 0; if ($count2 == "15") { break; } else { ?>
    
     <div class="post" id="post-<?php the_ID(); ?>">
    				<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    				<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
    
    <?php if( get_post_meta($post->ID, "homeimg", true) ): ?>
    <div class=”homeimg”>
    <img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "homeimg", $single = true); ?>&h=223&w=297&zc=0" alt="<?php the_title(); ?>" width="297" height="223" />
    </div>
    <?php else: ?>
    <?php endif; ?>
    
    				<div class="entry">
                                     <?php the_excerpt(); ?>
    				</div>
    
    			</div>
    
    <?php $count2++; } ?>
    <?php endforeach; ?>
    </div>
    
    <!---NEWS - LEFT COLUMN LOOP ENDS--->
    
    <!---NEWS - RIGHT COLUMN LOOP START--->
    
    	<div id="column_02">
    
    <?php query_posts('showposts=15'); ?>
    <?php $posts = get_posts('numberposts=15&offset=15'); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count3 = 0; if ($count3 == "15") { break; } else { ?>
    
     <div class="post" id="post-<?php the_ID(); ?>">
    				<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    				<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
    
    <?php if( get_post_meta($post->ID, "homeimg", true) ): ?>
    <div class=”homeimg”>
    <img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "homeimg", $single = true); ?>&h=223&w=297&zc=0" alt="<?php the_title(); ?>" width="297" height="223" />
    </div>
    <?php else: ?>
    <?php endif; ?>
    
    				<div class="entry">
                                     <?php the_excerpt(); ?>
    				</div>
    
    			</div>
    
    <?php $count3++; } ?>
    <?php endforeach; ?>
    </div>
    </div>
    
    <!---NEWS - RIGHT COLUMN LOOP ENDS--->
    
    	<?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    		<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    
    	<?php endif; ?>
    
    	</div>
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    I have tried adding &cat=-5,-16 to the query_posts string, but this is not working. Can anyone help.

  2. MichaelH
    Member
    Posted 4 years ago #

    Assuming you are talking about your 1st loop there called your feature loop, see http://codex.wordpress.org/The_Loop#Exclude_Posts_From_Some_Category

    Those other two query loops might be working for you, but you could consider using get_posts with setup_postdata, or a new WP_Query, instead of query_posts (which is really your 'main' query).

  3. DrGizmondo
    Member
    Posted 4 years ago #

    @MichaelH I dont understand, all I want to do is exclude posts from the feature section showing in the news section bellow.

  4. MichaelH
    Member
    Posted 4 years ago #

    You shouldn't need this kind of construct:

    <?php query_posts('showposts=15'); ?>
    <?php $posts = get_posts('numberposts=15&offset=0');

    Should be able to use just:

    <?php $posts = get_posts('cat=-4,-5&numberposts=15&offset=0');
  5. DrGizmondo
    Member
    Posted 4 years ago #

    Thanks MichaelH that works nicely.

Topic Closed

This topic has been closed to new replies.

About this Topic