WordPress.org

Ready to get started?Download WordPress

Forums

WP Query Pagination (6 posts)

  1. OlyReeve
    Member
    Posted 1 year ago #

    Hi all,

    I'm a php beginner and I'm having some problems with pagination. Can anyone help?

    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    	<?php global $post;
    		$args = array( 'posts_per_page' => 2, 'child_of' => 6, 'offset' => 1);
    		$myposts = get_posts( $args );
    		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    		foreach( $myposts as $post ) :	setup_postdata($post); ?>
    			<div class="articleOld">
    				<a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('medium'); } ?></a>
    				<h4><?php the_time('jS F Y') ?> in <?php the_category(', '); ?></h4>
    				<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    				<?php the_excerpt(); ?>
    			</div>
    
    	<?php endforeach; ?>
    <?php endwhile; ?>
    	<?php posts_nav_link(' — ', __('&laquo; Newer Posts'), __('Older Posts &raquo;')); ?>
  2. vtxyzzy
    Member
    Posted 1 year ago #

    You are using a get_posts() call inside the Loop. You should modify the Loop instead (UNTESTED):

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       $args = array( 'posts_per_page' => 2, 'child_of' => 6, 'offset' => 1, 'paged' => $paged);
       query_posts($args);
       if (have_posts()) : while (have_posts()) : the_post(); ?>
          <div class="articleOld">
             <a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('medium'); } ?></a>
             <h4><?php the_time('jS F Y') ?> in <?php the_category(', '); ?></h4>
             <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
             <?php the_excerpt(); ?>
          </div>
    <?php endwhile; endif; ?>
    <?php posts_nav_link(' — ', __('« Newer Posts'), __('Older Posts »')); ?>
  3. OlyReeve
    Member
    Posted 1 year ago #

    Thanks for your help, it's still not quite working right, here's my full code - it's messy :)

    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    	<?php global $post;
    		$args = array( 'numberposts' => 1, 'child_of' => 6);
    		$myposts = get_posts( $args );
    		foreach( $myposts as $post ) :	setup_postdata($post); ?>
    			<div class="article">
    				<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    				<h5><?php the_category(', '); ?></h5>
    				<h4>Written <?php the_time('jS F Y') ?> by <?php the_author_posts_link(); ?></h4>
    				<a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('large'); } ?></a>
    				<?php the_excerpt(); ?>
    			</div>
    	<?php endforeach; ?>
    <?php endwhile; ?>
    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       $args = array( 'posts_per_page' => 2, 'child_of' => 6, 'offset' => 1, 'paged' => $paged);
       query_posts($args);
       if (have_posts()) : while (have_posts()) : the_post(); ?>
          <div class="articleOld">
             <a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('medium'); } ?></a>
             <h4><?php the_time('jS F Y') ?> in <?php the_category(', '); ?></h4>
             <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
             <?php the_excerpt(); ?>
          </div>
    <?php endwhile; endif; ?>
    <?php posts_nav_link(' — ', __('« Newer Posts'), __('Older Posts »')); ?>

    and a link if you need to see whats going on, thanks!
    http://kitehub.co.uk/bude/beaches

  4. vtxyzzy
    Member
    Posted 1 year ago #

    What is the purpose of the first if (have_posts())... loop? It has the get_posts() call inside it and that is more than likely not what you want.

    Which part of the page are you wanting to paginate?

  5. OlyReeve
    Member
    Posted 1 year ago #

    Thanks for your patience. I just want the first post to appear differently to rest of the posts. I created two loops because I didn't know how to make the first one different. This is probably the reason it's messing up! Cheers!

  6. vtxyzzy
    Member
    Posted 1 year ago #

    OK. Just use a single loop and a counter to do something different for the first post.

    But that brings up another question: Do you want to do something different only on the first page, or every page? Since the answer is usually 'Only on the first page.', that is how the sample code below should work.

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       $args = array( 'posts_per_page' => 2, 'child_of' => 6, 'offset' => 1, 'paged' => $paged);
       query_posts($args);
       $counter = 0;
       if (have_posts()) : while (have_posts()) : the_post();
          if ( $paged == 1 && ++$counter == 1) {
             // Put the code for the first post below this line. ?>
          <?php } else {
             // Put the code for the other post below this line. ?>
       <?php endif;
    endwhile; endif; ?>
    <?php posts_nav_link(' — ', __('« Newer Posts'), __('Older Posts »')); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic