WordPress.org

Ready to get started?Download WordPress

Forums

Multiple loops avoiding duplicate posts (7 posts)

  1. Digitalminds
    Member
    Posted 4 years ago #

    I'm customizing the index.php file because I wanted to show posts on the frontpage with a different markup. This is what I'm trying to reach:

    1 full post (the most recent post)
    2 exercpts of te next to posts in two columns
    3 a list with titles of the next 3 posts

    Does anyone know how to get this? I tried some multiple loops but they didn't brought me further than a blank page in my browser while testing.

  2. jessn
    Member
    Posted 4 years ago #

    This might help you:
    http://perishablepress.com/press/2008/09/01/multiple-loops-and-multiple-columns-with-wordpress/

    Have the first loop only pull up one post (showposts=1)

    Have the second loop only pull up two posts, with an offset of one to ignore the post above (showposts=2&offset=1). Then instead of the_content use the_excerpt

    Have the third loop show three posts with an offset of three (showposts=3&offset=3) and don't show the_content, just the_title.

  3. vtxyzzy
    Member
    Posted 4 years ago #

    You will need an adaptation of the 'mullet loop'.

    You will need something like:

    <?php if (have_posts()) : ?>
       <?php $count = 0; ?>
       <?php while (have_posts()) : the_post(); ?>
          <?php $count++; ?>
          <?php if ($count == 1) : ?>
    
             // if this is the first post, process any code that is specified in this region
             // process any code specified in this region before the content of the first post
    
             <?php the_content(); ?> // display the full content of the first post only
    
             // process any code specified in this region after the content of the first post
    
          <?php elseif ($count < 4) : ?>
    
             // if this is the second or third post, process any code that is specified in this region
             // process any code specified in this region before the content of the second or third post
    
             <?php the_excerpt(); ?> // display the excerpt of the second and third posts
    
             // process any code specified in this region after the second and third posts
    
          <?php else : ?>
    
             // if this is fourth or more post, process any code specified in this region
             // process any code specified in this region before the content of each post
    
             <?php the_title(); ?> // display only the title for all other posts
    
             // for all posts after the third, process this code below each post
    
          <?php endif; ?>
    
          // for each post, including the first, process any code included here
          // any code output will be displayed below the content of every post
    
       <?php endwhile; ?>
    <?php endif; ?>
  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    Or in the most basic terms, a standard loop with a counter... ;)

    Never could understand the name... How does mullet relate?.. At least if we called it The Sesame Street loop we could have some form of relation between counting and the name.. :)

    I'm just rambling randomly of course, you can just ignore me.. :)

  5. Digitalminds
    Member
    Posted 4 years ago #

    Many thanks you guys! It's working. For other people, the code is as following:

    <?php get_header(); ?>
    
    	<?php if (have_posts()) : ?>
    
    		<?php while (have_posts()) : the_post(); ?>
    
    <?php query_posts('showposts=1'); ?>
    <?php $posts = get_posts('numberposts=1&offset=0'); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count1 = 0; if ($count1 == "1") { break; } else { ?>
    
    <?php the_title(); ?>
    <?php the_excerpt(); ?>
    
    <?php $count1++; } ?>
    <?php endforeach; ?>
    
    <div id="leftpost">
    <?php query_posts('showposts=1'); ?>
    <?php $posts = get_posts('numberposts=1&offset=1'); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count2 = 0; if ($count2 == "1") { break; } else { ?>
    
    <?php the_title(); ?>
    <?php the_excerpt(); ?>
    <?php $count2++; } ?>
    </div>
    <?php endforeach; ?>
    
    <div id="rightpost">
    <?php query_posts('showposts=1'); ?>
    <?php $posts = get_posts('numberposts=1&offset=2'); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count3 = 0; if ($count3 == "1") { break; } else { ?>
    
    <?php the_title(); ?>
    <?php the_excerpt(); ?>
    
    <?php $count3++; } ?>
    </div>
    <?php endforeach; ?>
    
    <ul>
    <?php query_posts('showposts=7'); ?>
    <?php $posts = get_posts('numberposts=7&offset=3'); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count4 = 0; if ($count4 == "7") { break; } else { ?>
    
    <li><a class="" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
    <?php $count4++; } ?>
    
    <?php endforeach; ?>
    </ul>  
    
    		<?php endwhile; ?>
    
    	<?php else : ?>
    
    		<h2><?php _e('The page you<code>re looking for doesn</code>t exist', 'blank'); ?></h2>
    		<div class="search-404">
    		<?php _e('Do you want to search for it?', 'blank'); ?><br />
    		<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    		</div>
    
    	<?php endif; ?>
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>
  6. vtxyzzy
    Member
    Posted 4 years ago #

    Mullet - as in hair style that trails off long in the back - maybe?

    Please mark this topic 'Resolved'.

  7. quemultimedia
    Member
    Posted 4 years ago #

    Greetings Guys,

    Is it possible to implement the above 'Mullet-Loop' into different categories?

    i.e. the first loop to show 4 posts from category 1, the second loop to show 3 posts from category 2, the third loop to show posts from yet another category and so on.

    I will greatly appreciate if someone would point me in the correct direction, have been going round in loops myself!

    Regards
    Q
    {newbie to WordPress}

Topic Closed

This topic has been closed to new replies.

About this Topic