WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] WP Query Pagination (18 posts)

  1. OlyReeve
    Member
    Posted 1 year ago #

    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();
    			?>
  2. martiniboy
    Member
    Posted 1 year ago #

    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();
    			?>
  3. OlyReeve
    Member
    Posted 1 year ago #

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

  4. martiniboy
    Member
    Posted 1 year ago #

    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.

  5. OlyReeve
    Member
    Posted 1 year ago #

    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!

  6. keesiemeijer
    moderator
    Posted 1 year ago #

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

  7. OlyReeve
    Member
    Posted 1 year ago #

    its a custom template file.

  8. esmi
    Forum Moderator
    Posted 1 year ago #

    Is this a secondary Loop or the only Loop?

  9. IMICreation
    Member
    Posted 1 year ago #

    try adding this in array
    'paged' => get_query_var('paged')

  10. OlyReeve
    Member
    Posted 1 year ago #

    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

  11. esmi
    Forum Moderator
    Posted 1 year ago #

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

  12. OlyReeve
    Member
    Posted 1 year ago #

    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.

  13. esmi
    Forum Moderator
    Posted 1 year ago #

    Replace:

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

    with query_posts($args); - then just use a "standard Loop".

  14. keesiemeijer
    moderator
    Posted 1 year ago #

    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

  15. OlyReeve
    Member
    Posted 1 year ago #

    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();?>
  16. esmi
    Forum Moderator
    Posted 1 year ago #

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

  17. OlyReeve
    Member
    Posted 1 year ago #

    Thanks guys that worked. Sorry keesiemeijer that was perfect. Thanks for your patience one and all. :)

  18. keesiemeijer
    moderator
    Posted 1 year ago #

    No problem :-)
    and thank you esmi.

Topic Closed

This topic has been closed to new replies.

About this Topic