• I have my index set up with two loops. The first loop shows the four latest posts from categories 3, 4, and 5. The second loop shows posts from categories 1 and 3. The problem is that every now and then, a post will show up as the first post in loop 1 which will match the first post in loop 2. In cases like these, I would like the first post in loop 1 to not show. I’ve attempted this (incorrectly) in my code below with $first_post = $id == 1; and if( $post->ID == $first_post ) continue;. If anyone can help me out with this, I would really appreciate it.

    <?php get_header(); ?>
    
    <div id="page">
    <div class="content">
       <?php
        $first_post = $id == 1;
    /************************* LOOP 1 Four Posts in a Carousel **********************/
        query_posts('cat=3,4,5&showposts=4');
    	while (have_posts()) : the_post();
    	if( $post->ID == $first_post ) continue;
    	$Thumbnail = get_post_meta($post->ID, 'Thumbnail', $single = true);
       ?>
    			<div id="carousel-post" style="border-right: 1px solid #dddddd; display:inline; float:left; height:170px; margin:0 0 20px 0; padding:5px; width:145px;">
    			<center><div class="carousel-thumb"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><img class="blog-avatar" width="100" height="100" src="<?php echo get_post_meta($post->ID, "Thumbnail", true);?>" /></a></div></center>
    			<h2 style="font-size:1.3em; margin:5px 0 0 0"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php if (strlen($post->post_title) > 60) {
    			echo substr(the_title($before = '', $after = '', FALSE), 0, 60) . '...'; } else {
    			the_title();
    			} ?></a></h2>
    			</div>
    
    	<?php endwhile;
    /************************* LOOP 1 Ends **********************/
    
    /************************* LOOP 2 Main Page Posts **********************/
        query_posts('cat=1,3');
        while (have_posts()) : the_post();
        update_post_caches($posts);
    	$Thumbnail = get_post_meta($post->ID, 'Thumbnail', $single = true);
    ?>
    			<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 class="postmetadata" style="border-bottom:1px dotted #888; padding-bottom:10px; padding-top:10px; line-height:18px;">
    				<!-- post author -->
    				by <?php the_author() ?>
    				<!-- the date and time -->
    				on <?php the_time(get_option('date_format')) ?>
    				<!-- post category
    				under <?php the_category(', ') ?>.-->
    				<div id="home-ratings" style="float:right;"><?php if(function_exists('the_ratings')) { the_ratings(); } ?></div>
    				</div>
    
    				<div class="entry">
    					<?php if($Thumbnail !== '') { ?>
    					<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><img src="<?php echo $Thumbnail;?>" width="150" style="border:2px solid #dfdfdf; float:right; margin-left:10px;" /></a>
    					<?php } ?>
    					<?php if (function_exists('tweetmeme')) echo tweetmeme(); ?>
    					<?php add_filter('excerpt_length', create_function('','return "75";')); ?>
    					<?php the_excerpt(); ?>
    				</div>
    
    				<div class="postmetadata">
    				<div style="text-align:right;" class="read-more"><a href="<?php the_permalink(); ?>" title="Read the rest of <?php the_title(); ?>" class="more-link">Read more &raquo;</a></div>
    				<?php if( function_exists('the_tags') )
    						the_tags(__('Tags: '), ', ', '<br />');
    				?>
    				<?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></div>
    			</div>
    	       <?php endwhile;
    /************************* LOOP 2 Ends **********************/
    		   ?>
    
    		   <div class="navigation">
    			<div style="text-align:left"><?php next_posts_link('&laquo; Older Entries') ?></div>
    			<div style="text-align:right"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    		   </div>
    
    </div>
    <?php get_sidebar(); ?>
    </div>
    <?php get_footer(); ?>
Viewing 10 replies - 1 through 10 (of 10 total)
  • One of the examples in https://codex.www.ads-software.com/The_Loop#Multiple_Loops might be useful.

    Thread Starter cpkid2

    (@cpkid2)

    Thanks esmi. That’s what I based my code on actually so I’m familiar with it. Basically, I just need to know what variable I should use to target the first post and omit it if necessary.

    How about replacing if( $post->ID == $first_post ) continue; with:

    $check = get_posts('cat=3&numberposts=1');
    if ( $check[0]->ID == $post->ID ) continue;
    Thread Starter cpkid2

    (@cpkid2)

    Thanks esmi! The code works; however, I’m confused as to HOW it works because in your code it says (‘cat=3&numberposts=1’). Can you explain so I can know for future reference?

    To be more specific, I don’t understand why you only put cat=3 and not the other categories, yet it still works for all first posts.

    $check = get_posts('cat=3&numberposts=1');

    Get 1 (the latest) post in category id 3.
    The only potential duplication is within category 3 based on the details you gave above (Loop 1: cats 3,4 & 5 – Loop 2: cats 1 &3)

    if ( $check[0]->ID == $post->ID ) continue;

    If the id of this post matches the post you’re about to display, the the post you’re about to display must be the latest post in Category id 3). So skip it and move onto the next post (continue:) because this post will be shown in the second loop.

    Does that make sense?

    Thread Starter cpkid2

    (@cpkid2)

    Yes, I think I understand. Thank you.

    I love it!

    I am playing with it for a while now to get this check to skip the first 2 posts, i can′t get it done.

    Can you tell me how to skip the first 2 posts and continue.

    thanks

    @mediabros

    assuming anything else is the same othersise:

    if this line:
    $check = get_posts('cat=3&numberposts=1');
    skips 1 post;

    then this line:
    $check = get_posts('cat=3&numberposts=2');
    should skip 2 posts.

    I had the same problem. Thankfully the problem got sorted.

    Arpan

    @alchymyth

    That was what i tried indeed, but it didn’t work out for me

    This is my code

    <?php while (have_posts()) : the_post(); ?>
    <?php $check = get_posts('numberposts=1'); ?>
    <?php if ( $check[0]->ID == $post->ID) continue; ?>

    I tried

    <?php while (have_posts()) : the_post(); ?>
    <?php $check = get_posts('numberposts=2'); ?>
    <?php if ( $check[0]->ID == $post->ID) continue; ?>

    but the this is still only skipping the first post and showing the second.

    When i change it to

    <?php while (have_posts()) : the_post(); ?>
    <?php $check = get_posts('numberposts=2'); ?>
    <?php if ( $check[1]->ID == $post->ID) continue; ?>

    It will skip the second post but show the first post

    so i figured out to do it like this.

    <?php while (have_posts()) : the_post(); ?>
    <?php $check = get_posts('numberposts=2'); ?>
    <?php if ( $check[0]->ID == $post->ID | $check[1]->ID == $post->ID) continue; ?>

    That is working for me.

    @arpankar,

    How did you solved your problem?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘multiple loops, if first post in loop 1 matches first post in loop 2, skip…’ is closed to new replies.