WordPress.org

Support

Support » Themes and Templates » [Resolved] Resetting post query after the first page.

[Resolved] Resetting post query after the first page.

  • I feel like this should be obvious to me, but maybe I’m just not getting enough sleep.

    I set up a blog where I wanted the first post to display by itself at the top of the page in a different style, then the rest of the posts would be below it in a new section. This is working great, but when I click the navigation for the next page, it just shows that same posts again. How do I have the navigation continue to the next posts like normal, even though I did a query to start the loop?

    Here’s my code.

    <?php $top_query = new WP_Query('showposts=1'); ?>
    <?php while($top_query->have_posts()) : $top_query->the_post(); ?>
    
    		<article class="post first">
                          //Post Content
    		</article><!--post-->
    
    <?php endwhile; ?>
    
    <div class="content">
    	<div class="container">
    		<section class="main">
    
    			<?php query_posts('offset=1'); ?>
    			<?php while(have_posts()) : the_post(); ?>
    
    				<article class="post">
                                         //Post Content
    				</article><!--post-->
    
    			<?php endwhile; ?> 
    
    			<div class="navigation"><h1><?php posts_nav_link('','« Back','More on the next page »'); ?></h1></div>
    
    		</section><!--main-->
Viewing 4 replies - 1 through 4 (of 4 total)
  • Oh, and I understand that I can just put a conditional tag on the first query to only display on the home page, but the second query is still going to start from the offset=1 position. And if I put it in the same conditional tag, the next navigation just starts from the beginning with post 1.

    Anyone? Or do you know of a better way to place the first post in a separate div and let the rest flow normally?

    Michael
    Forum Moderator

    @alchymyth

    only one loop, with conditional is_paged();

    example:

    <?php if( !is_paged() ) : the_post(); ?>
    
    		<article class="post first">
                          //Post Content
    		</article><!--post-->
    
    <?php endif; ?>
    
    <?php if( have_posts() ) : ?>
    <div class="content">
    	<div class="container">
    		<section class="main">
    
    			<?php while(have_posts()) : the_post(); ?>
    
    				<article class="post">
                                         //Post Content
    				</article><!--post-->
    
    			<?php endwhile; ?> 
    
    			<div class="navigation"><h1><?php posts_nav_link('','« Back','More on the next page »'); ?></h1></div>
    
    		</section><!--main-->
    </div>
    </div>
    <?php endif; ?>

    (untested)

    Brilliant. That worked perfectly. Thanks a ton!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] Resetting post query after the first page.’ is closed to new replies.