• Hi guys, I’m trying to add pagination to a page that lists the 5 most recent post in its respective category.

    I’m not sure where to proceed from here, adding the following code adds a previous and next link, but if you click it, it doesn’t really work. As you can see, the page incorrectly loops (multiple repeats of the same set of posts.

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
      'posts_per_page' => 3,
      'paged' => $paged
    );
    
    query_posts($args);
    ?>

    And Here is the full code.

    <?php
    /*
    Template Name: News Template
    */
    
    include 'header.php';
    
    include 'terminal.php'; 
    
     ?>
    
     	<div id="primary" class="site-content">
    		<div id="content" role="main">
    		<?php if ( have_posts() ) : ?>
    
    			<?php /* Start the Loop */ ?>
    
    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
      'posts_per_page' => 3,
      'paged' => $paged
    );
    
    query_posts($args);
    ?>
    
    	<div  class="topbannertext">NEWS</div>
    	<?php
    // The Query
    query_posts( $args ); ?>
    			<?php while ( have_posts() ) : the_post(); ?>
    
    <div class="news">
    <?php
    $myposts = get_posts( '' );
    foreach($myposts as $post) :
    setup_postdata($post);
    ?>
      <div class="post-item">
        <div class="post-info">
          <h2 class="post-title">
          <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
          <?php the_title(); ?>
          </a>
          </h2>
    	  <p class="post-date"><?php echo get_the_date(); ?></p>
        </div>
        <div class="post-content">
        <?php the_content(); ?>
        </div>
      </div>
    
    <?php endforeach; wp_reset_postdata(); ?>
    </div>			
    
    			<?php endwhile; ?>
    
    <?php next_posts_link(); ?>
    <?php previous_posts_link(); ?>
    
    			<?php// twentytwelve_content_nav( 'nav-below' ); ?>
    
    		<?php else : ?>
    
    			<article id="post-0" class="post no-results not-found">
    
    			<?php if ( current_user_can( 'edit_posts' ) ) :
    				// Show a different message to a logged-in user who can add posts.
    			?>
    				<header class="entry-header">
    					<h1 class="entry-title"><?php _e( 'No posts to display', 'twentytwelve' ); ?></h1>
    				</header>
    
    				<div class="entry-content">
    					<p><?php printf( __( 'Ready to publish your first post? <a href="%s">Get started here</a>.', 'twentytwelve' ), admin_url( 'post-new.php' ) ); ?></p>
    				</div><!-- .entry-content -->
    
    			<?php else :
    				// Show the default message to everyone else.
    			?>
    				<header class="entry-header">
    					<h1 class="entry-title"><?php _e( 'Nothing Found', 'twentytwelve' ); ?></h1>
    				</header>
    
    				<div class="entry-content">
    					<p><?php _e( 'Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve' ); ?></p>
    					<?php get_search_form(); ?>
    				</div><!-- .entry-content -->
    			<?php endif; // end current_user_can() check ?>
    
    			</article><!-- #post-0 -->
    
    		<?php endif; // end have_posts() check ?>
    <?php get_sidebar(); ?>
    		</div><!-- #content -->
    	</div><!-- #primary -->
    
    <?php get_footer(); ?>

    This is the website in question. https://goo.gl/Aa1ET

  • The topic ‘Pagination – incorrect looping and not functional’ is closed to new replies.