Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a page with multiple loops, but the first loop repeats on the second (or Older) page. I want to keep the formatting but stop the post from repeating.

You can see the problem here: http://violentology.com/blog/

In my index.php file, I have the code:

<table width="980" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td colspan="3" style="background-color:#000">
            <?php get_the_image( array( 'meta_key' => 'feature_img', 'size' => 'full size', 'height' => '450', 'image_class' => 'post-feature-1' ) ); ?></td>
        </tr>
        <tr><td height="10"></td></tr>
        <tr>
    <td valign="top">
    <div id="main-col">
    <?php get_template_part( 'loop', 'index' ); ?>
    </div>
    </td>
    <td valign="top" id="mainsidebartd">
    <div id="linksidebar">
    <?php get_sidebar(); ?>
    </div>
    </td>
    <td width="10px">&nbsp;</td>
        </tr>
    </table>`

In my loop, the code is as follows:

<!-- First Loop -->
<div id="first_post">
<?php $my_query = new WP_Query('posts_per_page=1');
  while ($my_query->have_posts()) : $my_query->the_post();
  $do_not_duplicate = $post->ID;?>
        <!-- GETTING POST TITLE -->
        <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>

        <!-- GETTING DATE AUTHOR AND COMMENTS -->
        <div class="date"><em><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></em> | <em><?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?></em>
        </div>
        <!-- GETTING ACTUAL POST -->
<?php if ( is_archive() || is_search() ) : // Only display excerpts for archives and search. ?>
        <?php the_excerpt(); ?>
<?php else : ?>
        <?php the_content(); ?>
<?php endif; ?>
  <?php endwhile; ?>
</div>

<!-- Second Loop -->
<?php /* Start the Loop. */ ?>
<?php if (have_posts()) : while ( have_posts() ) : the_post(); 
  if( $post->ID == $do_not_duplicate ) continue; ?>

<div id="singlepost">   <!-- POSITIONS INDIVIDUAL POSTS -->
<div class="post-box"><?php get_the_image( array( 'meta_key' => 'feature_img', 'size' => 'medium', 'height' => '200', 'image_class' => 'post-feature' ) ); ?></div>
        <!-- GETTING POST TITLE -->
            <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>

        <!-- GETTING DATE AUTHOR AND COMMENTS -->
        <div class="date"><em><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></em> | <em><?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?></em>
        </div>
        <!-- GETTING ACTUAL POST -->
<?php if ( is_archive() || is_search() ) : // Only display excerpts for archives and search. ?>
        <?php the_excerpt(); ?>
<?php else : ?>
        <?php the_content('Continue Reading',FALSE,''); ?>
<?php endif; ?>
</div>

<?php endwhile; // End the loop. ?>

<?php endif; ?>

        <div id="postnav" class="postnav">
            <?php next_posts_link('&laquo; Older') ?> | <?php previous_posts_link('Newer &raquo;') ?>

    </div>`
share|improve this question
add comment

2 Answers

You can use this

get_query_var('paged')

to check to see if you are viewing the home page, page 1, page 2 and so on. so you could put this around the first block so it doesn't show after the first page (which I think is what you want):

if ( isset( get_query_var('paged') ) === false )
{
  //do page 1 stuff here.
}

get_query_var() is just returning whether the "paged=?" bit appears in your URL, so it will return nothing for the home page. I'm not sure whether it returns null or "" for the home page, you'll have to check.

share|improve this answer
    
Thanks Steve. I understand in terms of your writing, but I'm not clear on where I should put your suggestion. I've done this, but it's not working. Could you help me a bit here? <pre> <?php get_query_var('paged') ?> <?php $my_query = new WP_Query('posts_per_page=1'); while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?> <?php if ( isset( get_query_var('paged') ) === false ) { ?> //do page 1 stuff <?php }; ?> <?php endwhile; ?>` –  Andrew Dec 9 '12 at 22:16
    
@Andrew ... sounds like you need to put that check suggested by SteveClaridge before the Loop, not inside it, and follow the code exactly. –  s_ha_dum Feb 13 '13 at 15:45
add comment

You should really Reset your post data like the examples in the codex Resetting Post Data

Check out the wp_query function in the Codex for more info.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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