WordPress.org

Forums

Adding a second loop on the home page (11 posts)

  1. haussamen
    Member
    Posted 4 years ago #

    Hi,

    I'm looking to keep categories on my home page (http://nmpolitics.net/index) 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:

    <?php
    }
    elseif ( $postcount > 0 && $postcount <= 14 ){
    //GETS NEXT 14 EXCERPTS
    ?>

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

  2. stvwlf
    Member
    Posted 4 years ago #

  3. haussamen
    Member
    Posted 4 years ago #

    Alright, so I'd use some form of this:

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

    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?

    Thanks.

  4. stvwlf
    Member
    Posted 4 years ago #

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

    To pull 5 posts from cat 11
    $recentPosts->query('cat=11&posts_per_page=5');

    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
    $recentPosts->query('cat=11&p=-37&posts_per_page=5');

    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 http://wordpress.org/extend/plugins/astickypostorderer/
    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

  5. haussamen
    Member
    Posted 4 years ago #

    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...

  6. stvwlf
    Member
    Posted 4 years ago #

    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
    $recentPosts->query('cat=11&posts_per_page=14');
    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();
       the_title();
       the_content();
    endwhile; ?>

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

    <?php
    query_posts(array('post__not_in' => $ids));
    while (have_posts()) : the_post();
      the_title();
      the_content();
    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
    http://wpengineer.com/1719/filter-duplicate-posts-in-the-loop/

  7. haussamen
    Member
    Posted 4 years ago #

    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:

    <?php //GETS NEXT HEADLINES
    		}
    		else {
    			ob_start();
    			echo '<li><a href="';
    			the_permalink();
    			echo '">';
    			the_title();
    			echo '</a></li>';
    			$links[] = ob_get_contents();
    			ob_end_clean();
    		}
    		$postcount ++;
    		}
    	}
    	else {
    ?>
    
    <?php } ?>
    
    <?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...

  8. stvwlf
    Member
    Posted 4 years ago #

    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.

  9. haussamen
    Member
    Posted 4 years ago #

    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.

  10. stvwlf
    Member
    Posted 4 years ago #

    Hi

    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.

  11. haussamen
    Member
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic