WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Resetting post query after the first page. (5 posts)

  1. iamharlan
    Member
    Posted 2 years ago #

    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-->
  2. iamharlan
    Member
    Posted 2 years ago #

    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.

  3. iamharlan
    Member
    Posted 2 years ago #

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

  4. 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)

  5. iamharlan
    Member
    Posted 2 years ago #

    Brilliant. That worked perfectly. Thanks a ton!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.