WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] WP Query Pagination

[Resolved] WP Query Pagination

  • Hi,

    I’m trying to add pagination to a wp_query loop but I can’t get it to work, if anyone can help it’ll be much appreciated! Here’s my code:

    <?php
                $args=array(
    			'post_type' => 'post',
    			'post_status' => 'publish',
    			'posts_per_page' => 2,
    			);
    
    			$my_query = null;
    			$my_query = new WP_Query($args);
    			if( $my_query->have_posts() ) { ?>
    			<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    				<h3 class="category"><?php the_category(', ') ?></h3>
    				<div class="blogContainer">
    					<div class="blogpiece">
    					<div class="titleMeta"><div class="metaDeets">by <?php the_author(); ?> | <?php the_time('g:i') ?> | <?php the_time('d/m/Y') ?></div><h6><?php the_title(); ?></h6></div>
    					<div class="mainText">
    						<?php the_excerpt(); ?>
    					</div>
    					<div class="readMorebox"><a href="<?php echo get_permalink(); ?>">» Read more</a></div>
    					</div>
    				</div>
    			<?php endwhile; ?>
    									<?php posts_nav_link(' — ', __('« Newer Posts'), __('Older Posts »')); ?>
    
    			<?php }
    			wp_reset_query();
    			?>
Viewing 15 replies - 1 through 15 (of 17 total)
  • Try

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1
                $args=array(
    			'post_type' => 'post',
    			'post_status' => 'publish',
    			'posts_per_page' => 2,
                            'paged' => $paged
    			);
    
    			$my_query = null;
    			$my_query = new WP_Query($args);
    			if( $my_query->have_posts() ) { ?>
    			<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    				<h3 class="category"><?php the_category(', ') ?></h3>
    				<div class="blogContainer">
    					<div class="blogpiece">
    					<div class="titleMeta"><div class="metaDeets">by <?php the_author(); ?> | <?php the_time('g:i') ?> | <?php the_time('d/m/Y') ?></div><h6><?php the_title(); ?></h6></div>
    					<div class="mainText">
    						<?php the_excerpt(); ?>
    					</div>
    					<div class="readMorebox"><a href="<?php echo get_permalink(); ?>">» Read more</a></div>
    					</div>
    				</div>
    			<?php endwhile; ?>
    									<?php posts_nav_link(' — ', __('« Newer Posts'), __('Older Posts »')); ?>
    
    			<?php }
    			wp_reset_query();
    			?>

    Thanks Martiniboy but i got an error:
    Parse error: syntax error, unexpected T_VARIABLE on line 15

    Whoops sorry missed ; off, that teach me using my phone to answer
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    but tested when I got back to my computer and not the solution sorry.
    I did have this problem a few months back I will need to look through my code of old sites when I am at work tomorrow. Sorry was not any help.

    Hey Matiniboy.

    Thanks for trying did you have any luck in the end? I’ve tried for the life of me to get it working but no luck!

    Thanks!

    Moderator keesiemeijer

    @keesiemeijer

    On what theme template file are you trying to get pagination to work (index.php, category.php etc…)?

    its a custom template file.

    esmi

    @esmi

    Forum Moderator

    Is this a secondary Loop or the only Loop?

    try adding this in array
    ‘paged’ => get_query_var(‘paged’)

    It’s a single loop. This is how my code looks with your nitin_8, it still doesn’t work:

    <div class="main-content">
                        <?php
                $args=array(
    			'post_type' => 'post',
    			'post_status' => 'publish',
    			'posts_per_page' => 10,
    			'paged' => get_query_var('paged')
    			);
    
    			$my_query = null;
    			$my_query = new WP_Query($args);
    			if( $my_query->have_posts() ) { ?>
    			<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    				<h3 class="category"><?php the_category();?> </h3>
    				<div class="blogContainer">
    					<div class="blogpiece">
    					<div class="titleMeta"><div class="metaDeets">by <?php the_author_posts_link(); ?> | <?php the_time('g:i') ?> | <?php the_time('d/m/Y') ?></div><h6><?php the_title(); ?></h6></div>
    					<div class="mainText">
    						<?php the_excerpt(); ?>
    					</div>
    					<div class="readMorebox"><a href="<?php echo get_permalink(); ?>">&raquo; Read more</a></div>
    					</div>
    				</div>
    			<?php endwhile; ?>
    									<?php posts_nav_link(' — ', __('&laquo; Newer Posts'), __('Older Posts &raquo;')); ?>
    
    			<?php }
    			wp_reset_query();
    			?>
    
                    </div>

    Thanks

    esmi

    @esmi

    Forum Moderator

    You should be using query_posts() – not WP_Query() – for the main loop/query,

    I wish I knew how to! I’m an amateur and wouldn’t know hot to rewrite the code with query_posts(). Thanks for the advice though esmi.

    esmi

    @esmi

    Forum Moderator

    Replace:

    $my_query = null;
    $my_query = new WP_Query($args);

    with query_posts($args); – then just use a “standard Loop”.

    Moderator keesiemeijer

    @keesiemeijer

    Is this on a Page template, custom page template, static front page or on another theme template file?

    Try it with this:

    <div class="main-content">
                        <?php
                        if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }
    
                $args=array(
    			'post_type' => 'post',
    			'post_status' => 'publish',
    			'posts_per_page' => 10,
    			'paged' => $paged
    			);
    
    			query_posts($args);
    			if( have_posts() ) : ?>
    			<?php while (have_posts()) : the_post(); ?>
    				<h3 class="category"><?php the_category();?> </h3>
    				<div class="blogContainer">
    					<div class="blogpiece">
    					<div class="titleMeta"><div class="metaDeets">by <?php the_author_posts_link(); ?> | <?php the_time('g:i') ?> | <?php the_time('d/m/Y') ?></div><h6><?php the_title(); ?></h6></div>
    					<div class="mainText">
    						<?php the_excerpt(); ?>
    					</div>
    					<div class="readMorebox"><a href="<?php echo get_permalink(); ?>">» Read more</a></div>
    					</div>
    				</div>
    			<?php endwhile; ?>
    									<?php posts_nav_link(' — ', __('« Newer Posts'), __('Older Posts »')); ?>
    
    			<?php endif; ?>
    			<?php wp_reset_query(); ?>
    
                    </div>

    see: http://codex.wordpress.org/Pagination#Troubleshooting_Broken_Pagination

    Thanks for your help people, Kessitemeijer – it didn’t work, sorry. The page is a custom page template.

    Esmi I changed what you said but it’s given me an error, have I missed something?

    <?php
    /*
    Template Name: Blog Template
    */
    get_header(); ?>
    <div id="main">
              <div class="main-section">
                    <div class="headline">
                        <div class="holder">
                            <h1>Blog</h1>
                        </div>
                    </div>
                    <div class="main-content">
                        <?php
                $args=array(
    			'post_type' => 'post',
    			'post_status' => 'publish',
    			'posts_per_page' => 10,
    			'paged' => get_query_var('paged')
    			);
    
    			query_posts($args);
    			if( $my_query->have_posts() ) { ?>
    			<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    				<h3 class="category"><?php the_category();?> </h3>
    				<div class="blogContainer">
    					<div class="blogpiece">
    					<div class="titleMeta"><div class="metaDeets">by <?php the_author_posts_link(); ?> | <?php the_time('g:i') ?> | <?php the_time('d/m/Y') ?></div><h6><?php the_title(); ?></h6></div>
    					<div class="mainText">
    						<?php the_excerpt(); ?>
    					</div>
    					<div class="readMorebox"><a href="<?php echo get_permalink(); ?>">&raquo; Read more</a></div>
    					</div>
    				</div>
    			<?php endif; ?>
    									<?php posts_nav_link(' — ', __('&laquo; Newer Posts'), __('Older Posts &raquo;')); ?>
                    </div>
                </div>
    </div>
    <?php get_footer();?>
    esmi

    @esmi

    Forum Moderator

    Try dropping keesiemeijer’s code into your custom page template.

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘[Resolved] WP Query Pagination’ is closed to new replies.
Skip to toolbar