Support » Plugins » Hacks » Adding a second loop on the home page

  • Hi,

    I’m looking to keep categories on my home page ( that appear in my sidebar from appearing in the center column on the home page. In short, I use the category “commentary” and related categories in the sidebar, and the category “news and analysis” in the center column. In addition, I need to be able to pull from either category into the top story position using the sticky post option.

    So I think what I need to do is put a second loop specifically for the center column that limits it to the news and analysis category (category 11). But I’m not sure how to do it. Here’s the index.php code:

    [code moderated as per forum rules – please use the pastebin]

    The center columns posts below the sticky post start with this code:

    <div id="more-posts">

    I think the second loop that includes only certain categories for the center column should go around this code:

    elseif ( $postcount > 0 && $postcount <= 14 ){

    But at that point I’m in over my head. Can anyone help me?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Alright, so I’d use some form of this:

    <h3>Recent Articles</h3>
        $recentPosts = new WP_Query();
    <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
        <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    <?php endwhile; ?>

    But what code would I put in there to only draw from category 11? And what code would I use to exclude the sticky post?


    $recentPosts->query('showposts=5'); will pull 5 posts. should actually be (in current WP syntax)

    To pull 5 posts from cat 11

    As far as sticky posts, they only work as sticky posts on the designated posts page. I suspect the sticky posts feature is not going to work as it does on the home page when used within a custom query.

    I’m not sure if this syntax will work with the cat parameter. If it does it would look like this

    where 37 is the post ID of the post you want to exclude. minus 37 means do not include post ID 37.

    Another thing you could check is someone on this forum told me the other day they were using this plugin
    to set the post order, to keep one post at the top (a pseudo sticky post when not on home page), that it worked in WP 3 although its listed as good through 2.9.2

    OK, I’m not sure what you mean on sticky posts. This is a custom query on the home page, and all I want to do is somehow exclude the sticky post from the custom query so the sticky posts isn’t repeated in the custom query.

    Here’s the code I have now:

    [code moderated as per forum rules – please use the pastebin]

    It limits the posts in that center column that include a post summary to category 11. Now I also need to figure out how to incorporate it into that section below, headlined “Older posts.” I need them to be part of the same loop so that the second doesn’t repeat the headlines in the first.

    I tried moving the

    <?php endwhile; ?>

    code down below the “Older posts” section, and that crashed my site…

    Here is some code that might help you. This is copied from an old article. I’ve not adapted it to your specifics.

    you’d use
    instead of the query_posts referenced in the code below


    More Than One Loop On A Page, Without Printing Duplicate Posts

    1. first loop: get the eight most recent posts using the posts_per_page parameter. Open the index.php file, and paste the following code to output your “featured” posts:

    <?php query_posts('posts_per_page=8');
    $ids = array();
    while (have_posts()) : the_post();
       $ids[] = get_the_ID();
    endwhile; ?>

    2. apply second loop and get all posts, excepted the ones we have already outputted in the first loop:

    query_posts(array('post__not_in' => $ids));
    while (have_posts()) : the_post();
    endwhile; ?>

    Code explanation:
    The first loop starts with the very useful query_posts() function, which allows you to specify a wide range of parameters to be used by the loop. The showposts parameter allows you to get the specified number of posts. Just before the loop starts, I create a PHP array named $ids, which will receive all IDs of the posts returned by this loop.

    Like the first one, the second loop uses the query_posts() function with the post__not_in parameter. This parameter allows you to specify a list of posts that you don’t want to be displayed, in the form of a PHP array. As you probably saw, I passed the $ids array to this parameter so that any posts returned by the first loop would be returned again by the second loop.

    another version

    Filter Duplicate Posts in the Loop

    Thanks. I think my problem now is that I don’t understand this part of the code enough to even build a second loop into it:

    		else {
    			echo '<li><a href="';
    			echo '">';
    			echo '</a></li>';
    			$links[] = ob_get_contents();
    		$postcount ++;
    	else {
    <?php } ?>
    	if(count($links)): ?>
    	 <h3><?php _e('Older Posts','Mimbo'); ?></h3>
    	 <ul class="headlines"><?php echo join("\n", $links); ?></ul>
    	<?php endif; ?>

    Everything I’m trying is crashing the site. I’m not sure how this code even pulls headlines to begin with…

    The answer is, unless you left out some code that precedes yoiur recentPosts loop, that code doesn’t pull in headlines. Its not attached to a query. And, it is code that is in the middle of an IF-ELSE statement, and you are not in an IF-ELSE statement when you are executing it. Thus its not working.

    Why don’t you exclude category 11 from your sidebar query and include only category 11 in you center column query. Doesn’t that resolve your issue?

    I’m sorry I don’t have time right now to get any further into this. You can respond and I will answer when I have time.

    When I include only category 11 in the center column, it also excludes other categories in the sidebar. I haven’t been able to figure out why.

    There’s no <?php endwhile; ?> in my current code in the index page. Is that why? If so, where would I put that code? Here’s the full code:

    [code moderated as per forum rules – please use the pastebin]

    If I add <?php endwhile; ?> somewhere in this, I assume I’m cutting off a loop that extends beyond the index page and have to add code somewhere else. Is that right?

    I greatly appreciate your help.


    The reason there is no endwhile is because the while loop is only one line long. It consists of the IF line. All the logic is in the IF statement. All the loop does is execute the IF statement on each iteration of the loop. This is a very convoluted way to code that is really difficult to trace and debug, as you have discovered.

    The ob_start stuff is buffered output. There is no reason that needs to be used in a while loop that uses direct logic.

    When I include only category 11 in the center column, it also excludes other categories in the sidebar. I haven’t been able to figure out why.

    That is because you only have one loop, thus there is only one query. All the restricting parameters applied for the first column are thus also applied to the other column, which is exactly what you don’t want. There is no way around that in just one loop without some very convoluted code.

    I highly suggest you start over, with a different strategy. The code I pasted above that uses two loops and excludes posts included in the 1st loop from appearing in the second loop is a good starting place for you.

    Man, I thought the Mimbo theme was supposed to be a pretty good one… Sigh. Thanks for your help. Back to the drawing board. I may ask another question or two over the next couple of days, if that’s OK.

    Thanks so much for your help.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Adding a second loop on the home page’ is closed to new replies.