• I’m not that great in PHP, but I can get by. Right now I’ve got two separate loops going and I really need three different ones — unless you can help answer my question. My first loop, the one that is currently stand alone, brings up the most resent post from any category except #16. The beginning of the second loop bring up the most recent post in category #16. Both of these work fine but here is where things get tricky. The third call needs to be the offset by the most recent post and not include anything from category #16. So in the current state I have it offsetting 2 posts but if I were to post something to category #16 before anywhere else this system would be off. I tried three separate loops and the problem is that query_posts doesn’t allow for the offsetting of posts and while get_posts does, it doesn’t allow for excluding a category and I can’t seem to get the two to work hand in hand.
    <div id="story">
    <?php query_posts('showposts=1&cat=-16'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div id="recient">
    <div id="post-<?php the_ID(); ?>">
    <h1>" title="Read <?php the_title(); ?>"><?php the_title(); ?></h1>
    <h4>Posted <?php if (function_exists('time_since')) { echo time_since(abs(strtotime($post->post_date_gmt . " GMT")), time()) . " Ago"; } else { the_time('F jS, Y'); } ?> in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?></h4>
    <?php the_content('', true, ''); ?>
    <h3>Continue Reading /#more-<?php the_ID(); ?>" title="Continue Reading <?php the_title(); ?>">"<?php the_title(); ?>"</h3>
    </div>
    </div>
    <?php endwhile; else: endif; ?>
    <?php query_posts('showposts=1&cat=16'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div id="feature">
    <div id="post-<?php the_ID(); ?>">
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>
    <?php edit_post_link('Edit', '', ''); ?>
    </div>
    </div>
    <?php endwhile; ?>
    </div>
    <div id="headlines">
    <?php $posts = get_posts('numberposts=3&offset=2'); foreach($posts as $post) : setup_postdata($post); ?>
    <h3>"><?php the_title(); ?></h3>
    <h4><?php the_time('F jS, Y'); ?> | "><?php comments_number('No Comments','One Comment','% Comments'); ?></h4>
    <?php the_excerpt(); ?>
    <?php endforeach; ?>
    </div>
    <?php else: endif; ?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • Make this your third loop:

    <?php query_posts('showposts=6&cat=-16'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php if($posts[0]->ID != $post->ID) : ?>

    ~ ‘The Loop’ template tags, HTML etc. go here ~

    <?php endif; endwhile; endif; ?>

    It’s effectively the same as your first except we’re testing on whether we are at the first post or not by comparing the first post object’s ID ($posts[0]->ID) with the current ones. Make certain to add one more post to ‘showposts’ than you plan to display, to deal with your “offset.”

    Thread Starter jdcfsu

    (@jdcfsu)

    That works, but it needs to do one more thing, eliminate all posts from category 16 and drop back one as well. The first loop outputs the newest post not in 16, the second loop outputs the newest post in 16, and the third needs to output the next 5 not covered by the first two. Would there be a way to combine all the loops and have it requery and use the ($posts[0]->ID) along the way as needed?

    …but it needs to do one more thing, eliminate all posts from category 16 and drop back one as well.

    Uh, it should be doing that. It performs exactly as your first loop query, which is negating category 16. It’s just showing more posts, bypassing the very first one (which is displayed by your first loop). The logic of the loop I provided is:

    1. Get 6 posts
    2. But none from category 16
    3. Don’t display the first post (so in the end display only 5)

    This appears to be what you originally asked for.

    Thread Starter jdcfsu

    (@jdcfsu)

    Perhaps I am implimenting it wrong because as it stands, I am still getting the post from the first loop. It correctly excludes the one found in category 16, but still does not offset one further. A brief look at my code is as follows:
    <?php query_posts(‘showposts=1&cat=-16’); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    // Loop 1
    <?php endwhile; else: endif; ?>
    <?php query_posts(‘showposts=1&cat=16’); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    // Loop 2
    <?php endwhile; else: endif; ?>
    <?php query_posts(‘showposts=3&cat=-16’); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php if($posts[0]->ID != $post->ID) : ?>
    // Loop 3
    <?php endif; endwhile; endif; ?>

    It seems every new encounter with the posts object is just a lesson in humiliation…

    Change the start of your first loop to:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); $firstpost = $post->ID; ?>

    Then change the if() in the third loop to:

    <?php if($firstpost != $post->ID) : ?>

    Now, I’m off to throttle $posts for a bit.

    Thread Starter jdcfsu

    (@jdcfsu)

    That works! Thank you. It seems like every step I take in PHP comes with two backwords. Thank you again.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘3 Loops? I need help.’ is closed to new replies.