Modify WordPress Loop to Show Posts from Only One Category
-
Hi –
I’m trying to modify the WordPress loop on the home page of a new website.
Currently, the lead section displays every post, but I’d like to only include posts from a featured category. The loop already uses some WPQuery calls later on down the page as it lists posts in a category. Specifically, I’d like the ability to tell the loop to display the posts based on category number.
If anyone can give me some advice on what to change to make this happen, I’d be most grateful!
Here’s the entire index template:
<?php include (TEMPLATEPATH . '/header.php'); ?> <?php global $visiblePostIDs; $visiblePostIDs = ''; ?> <div id="content"> <h2 class="blocktitle"><span>Figure out something to go here</span><a href="<?php echo $feedURL; ?>" class="ico-rss"><img src="<?php bloginfo('template_directory'); ?>/images/feed.png" alt="RSS: News Maker" /></a></h2> <div id="lastpost" class="leftcol"> <?php if (have_posts()) : the_post() ?> <?php $visiblePostIDs .= $post->ID . ","; ?> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/thumb.php?src=<?php echo get_post_meta($post->ID, "image", $single = true); ?>&w=450&h=300&zc=1&q=95" alt="<?php the_title(); ?>" id="post-image" /></a> <div class="snippet leftcol"> <h2 class="title"><a href="<?php the_permalink(); ?>" title="<?php the_title() ?>" rel="bookmark"><?php the_title(); ?></a></h2> <div class="excerpt"><?php the_excerpt(); ?></div> <p class="readmore"><a href="<?php the_permalink() ?>">Read more</a><br /><br /><br /<img src="http://www.onthebaseline.com/uploads/Live-scoring.jpg" alt="Live Tennis Scores" height="Live Tennis Scores" width="218" height="100" /></p> </div> <div id="similar" class="rightcol"> <h3>Breaking Tennis News</h3> <ul class="listposts"> <?php delicious_bookmarks('onthebaseline', 5, true, true, false, '', false, '', false, false); ?></ul<br /> </div> <?php endif; ?> </div> <div id="latestposts" class="rightcol"> <?php query_posts('showposts=4&offset=1'); while ( have_posts() ) : the_post(); $visiblePostIDs .= $post->ID . ","; ?> <div class="snippet"> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/thumb.php?src=<?php echo get_post_meta($post->ID, "image", $single = true); ?>&w=160&h=100&zc=1&q=95" alt="<?php the_title(); ?>" class="alignleft" /></a> <h3 class="title"><a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark"><?php the_title() ?></a></h3> <div class="excerpt"><?php the_excerpt(); ?></div> </div><!-- .snippet --> <?php endwhile ?><img src="http://www.onthebaseline.com/uploads/NewTheme/Twitter.jpg" /> <img src="http://www.onthebaseline.com/uploads/NewTheme/Facebook.jpg" /> </div> <?php include (TEMPLATEPATH . '/featured.php'); ?> <div id="featuredcats"> <?php $homepageCatIDs = explode (',', $homepageCatIDs); $counter = 1; foreach ($homepageCatIDs as $catID) { ?> <?php if ($counter % 2 != 0) { ?><div class="catrow"><?php } ?> <div class="catblock<?php if ($counter % 2 == 0) echo ' catblockmargin' ?>"> <h2 class="blocktitle"><span><?php echo get_cat_name($catID); ?></span><a href="<?php echo get_category_link($catID); ?>" class="cat-more">More <?php echo get_cat_name($catID); ?> »</a></h2> <?php $catPosts = new WP_Query(); $excludePostIDs = explode (',', $visiblePostIDs); $catPosts->query(array('post__not_in'=>$excludePostIDs,'showposts'=>1,'cat'=>$catID)); ?> <div class="snippet"> <?php if ( $catPosts->have_posts() ) : $catPosts->the_post() ?> <?php $visiblePostIDs .= $post->ID . ","; ?> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/thumb.php?src=<?php echo get_post_meta($post->ID, "image", $single = true); ?>&w=180&h=120&zc=1&q=95" alt="<?php the_title(); ?>" class="featuredcats-image" /></a> <h3 class="title"><a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark"><?php the_title() ?></a></h3> <div class="excerpt"><?php the_excerpt(); ?></div> <?php endif; ?> </div> <?php $catPosts = new WP_Query(); $catPosts->query(array('post__not_in'=>$excludePostIDs,'showposts'=>3,'cat'=>$catID,'offset'=>1)); ?> <ul class="listposts"> <?php while ( $catPosts->have_posts() ) : $catPosts->the_post() ?> <?php $visiblePostIDs .= $post->ID . ","; ?> <li><a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark"><?php the_title() ?></a></li> <?php endwhile ?> </ul> </div> <?php if ($counter % 2 == 0) { ?></div><?php } ?> <?php $counter++; } ?> <?php if ($counter % 2 == 0) { ?></div><?php } ?> INSERT ADDITIONAL CONTENT HERE </div> </div><!-- #content --> <?php get_footer() ?>And here is the specific part of the code that I want to change (it’s posted above, but this is the area where I want to include the featured articles only):
<div id="lastpost" class="leftcol"> <?php if (have_posts()) : the_post() ?> <?php $visiblePostIDs .= $post->ID . ","; ?> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/thumb.php?src=<?php echo get_post_meta($post->ID, "image", $single = true); ?>&w=450&h=300&zc=1&q=95" alt="<?php the_title(); ?>" id="post-image" /></a> <div class="snippet leftcol"> <h2 class="title"><a href="<?php the_permalink(); ?>" title="<?php the_title() ?>" rel="bookmark"><?php the_title(); ?></a></h2> <div class="excerpt"><?php the_excerpt(); ?></div> <p class="readmore"><a href="<?php the_permalink() ?>">Read more</a><br /><br /><br /<img src="http://www.onthebaseline.com/uploads/Live-scoring.jpg" alt="Live Tennis Scores" height="Live Tennis Scores" width="218" height="100" /></p> </div> <div id="similar" class="rightcol"> <h3>Breaking Tennis News</h3> <ul class="listposts"> <?php delicious_bookmarks('onthebaseline', 5, true, true, false, '', false, '', false, false); ?></ul<br /> </div> <?php endif; ?> </div> <div id="latestposts" class="rightcol"> <?php query_posts('showposts=4&offset=1'); while ( have_posts() ) : the_post(); $visiblePostIDs .= $post->ID . ","; ?> <div class="snippet"> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/thumb.php?src=<?php echo get_post_meta($post->ID, "image", $single = true); ?>&w=160&h=100&zc=1&q=95" alt="<?php the_title(); ?>" class="alignleft" /></a> <h3 class="title"><a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark"><?php the_title() ?></a></h3> <div class="excerpt"><?php the_excerpt(); ?></div> </div><!-- .snippet -->Thanks so much for the help!
The topic ‘Modify WordPress Loop to Show Posts from Only One Category’ is closed to new replies.