• Hi,

    I’m trying to create multiple loops on single.php so that the single post is shown on the top of the page, and the excerpt from the next post is shown below this. I’ve had no luck so far. I’ve tried query_posts with an offset of 1 (just gives me the 2nd most recent post) and I’ve tried coding a second loop without resetting the first, but just gives me the same post again.

    Would anyone know a workaround?

    Thanks,

    Nick

Viewing 5 replies - 1 through 5 (of 5 total)
  • You will need 2 Loops. In the first Loop (showing the appropriate single post), grab the id of the post as a variable. In the second Loop, try setting up a custom query using the post id variable +1.

    Thread Starter nickbudden

    (@nickbudden)

    Hi,

    Sorry, but variables are still a little bit beyond my skills. I’ve tried quite a few things with variables over the last hour but haven’t had any luck. Here is my latest attempt, which I haven’t been able to get working.

    Beginning of first loop:

    <?php if ( have_posts() ) : ?>
    	 <?php while ( have_posts() ) : the_post(); ?>
                <?php $addone = <?php the_ID(); ?>;
    		  $nextpost = $addone+1;
                ?>

    End of first loop and beginning of second loop:

    <?php endwhile; ?>
    	<?php endif; wp_reset_query(); ?>
    
    <?php $my_query = new WP_Query('showposts=1&p=$nextpost;'); ?>
       <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

    Is this along the lines of what you had meant?

    Thanks again,

    Nick

    It looks about right – although you might want to try:

    <?php $my_query = new WP_Query('posts_per_page=1&p=' . $nextpost); ?>

    Also, this is pure theory. I’ve never tried it myself.

    Thread Starter nickbudden

    (@nickbudden)

    Didn’t work, but thanks for the effort.

    http://codex.ww.wp.xz.cn/Function_Reference/get_adjacent_post

    to check it, make a print out of the whole object:

    <?php print_r( get_adjacent_post(false,'',true); ?>

    i think, the id of the prev post is:
    <?php $prev_id = get_adjacent_post(false,'',true)->ID; ?>

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘multiple loops on single.php?’ is closed to new replies.