0

This is my archive page codes

i want to avoid duplicate posts in 4th loop & i want to count post in 3rd loop

there are 4 loops

1 first loop count : 1 post

  <?php 
  $count = 1;
  if (have_posts()) : while (have_posts()) : the_post();            
  if($count == 1) : ?>

 <a href="<?php the_permalink() ?>" rel="bookmark" title="" ><?php the_title(); ?></a> 

2 second loop - - count = 1 post

<?php if (have_posts()) : ?>
<?php $count = 1; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 2) : ?> 
<a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?>  </a> 

3 third loop-- count = 5 post

<?php else : ?> 
<a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?></a> 
<?php endif; ?>
<?php endwhile; ?>

<?php else : ?>
<h1>Most Viewed News</h1>
<?php endif; ?>

4 loop -- count = rest of all post

<?php else : ?> 
<a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?></a> 

-- ad count --

<?php if ( $count == 3 || $count == 5 ) : ?>
<?php dt_show_ads();?> 
<?php endif; ?>

<?php endif; ?> 
<?php $count++; ?> 
<?php endwhile; ?> 


<?php else : ?>
<div class="post">
<h2 class="archiveTitle"><?php _e('Sorry','linepress');?></h2>
</div>
<?php endif; ?>

2 Answers 2

1

I would avoid this kind of excess indentation at all costs, however this should work although I haven't tested it. Please let me know if there are syntax errors so that I can correct them.

<!-- first loop : 1 post -->        
<?php
    if ( have_posts() ):
        the_post(); ?>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="" ><?php the_title(); ?></a> 

    <!-- second loop : 2 posts -->
    <?php
        if ( have_posts() ):
            $count = 0;
            while ( have_posts() ):
                the_post(); ?>
                <a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?></a> 

                <!-- third loop : 5 posts -->
            <?php
                if ( ++$count == 2 and have_posts() ):
                    $count = 0;
                    while ( have_posts() ):
                        the_post(); ?>
                        <a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?></a> 

                        <!-- fourth loop : the rest -->
                    <?php
                        if ( ++$count == 5 and have_posts() ):
                            while ( have_posts() ):
                                the_post(); ?>
                                <a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?></a>
                        <?php
                            endwhile;
                        else: // fourth loop has no posts
                        endif;
                    endwhile;
                else: // third loop has no posts
                endif;
            endwhile;
        else: // second loop has no posts
        endif;
    else: // first loop has no posts
    endif;
?>

A more elegant solution would be something along these lines:

<?php
    $page_counts = array(1, 2, 5, 9999);
    foreach ($page_counts as $iteration => $max_count) {
        $count = $max_count;
        while ( have_posts() and $count-- ) {
            the_post(); ?>
            <a href="<?php the_permalink() ?>" title="" ><?php the_title(); ?></a> 
    <?php
        } else break; // No more posts to grab!
    }
?>

Again, untested, may contain a mismatching brace or something.

13
  • but post is duplicate in 4 loop i want to avoid duplicate posts
    – user8503
    Oct 18, 2011 at 7:25
  • Are you sure? You're not rewinding the loop between posts, so there should be no reason for the appearance of duplicate posts, unless your original query contains them. What is the original query?
    – soulseekah
    Oct 18, 2011 at 7:28
  • this is a archive page
    – user8503
    Oct 18, 2011 at 7:30
  • It's still a regular loop, and it's not being rewinded, again, what is the orginal query for the page var_dump($wp_query->query_vars);
    – soulseekah
    Oct 18, 2011 at 7:49
  • please have a look pastebin.com/4YgsAZjM
    – user8503
    Oct 18, 2011 at 7:53
0

For your 4th loop you can possibly use another query_post and offset by 7 (the first 7 posts in your other loops)..which will display the remaining posts.

like so:

<?php query_posts('offset=7'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>  
3
  • ok & how to limit the 5 posts in 3 third loop ?
    – user8503
    Oct 18, 2011 at 7:00
  • Probably just set this in your first query_post: '<?php query_posts('showposts=7'); ?>' Oct 18, 2011 at 7:17
  • there are syntax error
    – user8503
    Oct 18, 2011 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.