Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pagination on custom post wp_query

<?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $loop = new WP_Query(
            array(
                'post_type' => 'html5-blank',
                'posts_per_page' => 5,
                'paged'=>$paged
            )
        );
?>
<?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>      
 //Loop Code Here..
 <?php wp_reset_query(); ?> 
   <nav>
        <?php previous_posts_link( 'Newer posts &raquo;' ); ?>
        <?php next_posts_link('Older &raquo;') ?>
    </nav>
<?php endwhile; ?>
<?php else: ?>

Url on next page as I input result is: www.mywebsite.com/blog/page/2 is WORKING. But I can't show the pagination links.

Where did it go wrong?

EDIT: Pagination link is showing in page/2/ but not in the main blog page. Why?

like image 681
woninana Avatar asked Jan 30 '13 02:01

woninana


People also ask

How do I include pagination in a custom post type query in WordPress?

Pagination Note: You should set get_query_var( 'page' ); if you want your query to work with pagination. Since WordPress 3.0. 2, you do get_query_var( 'page' ) instead of get_query_var( 'paged' ). The pagination parameter 'paged' for WP_Query() remains the same.

How do I add custom pagination to my WordPress blog?

In order to add pagination to a WordPress theme, we need to build a function which will output previous and next post links at the bottom of the page, then add that to our template page. This is similar to the “Older Entries” and “Newer Entries” links that we saw above.

How do I get pagination on WordPress?

WordPress provides several functions for automatically displaying a numerical pagination list. If you want more robust pagination options, you can use the_posts_pagination() for WordPress 4.1 and higher. This will output a set of page numbers with links to previous and next pages of posts.


2 Answers

I think you put <?php wp_reset_query(); ?> in the wrong place.. shouldn't it be next or after pagination codes?

something like this

<?php endwhile; ?>
<?php else: ?>
<?php wp_reset_query(); ?>
like image 195
Reigel Avatar answered Oct 01 '22 02:10

Reigel


This question was answered very adequately by @Trevor but I needed to implement numbered pagination, and there was a bit more research to do. I hope my code helps others implement numbered pagination.

    <div class="frontpage-posts">
        <?php
        if (get_query_var('paged')) {
          $paged = get_query_var('paged');
        } elseif (get_query_var('page')) {
          $paged = get_query_var('page');
        } else {
          $paged = 1;
        }
        $temp = $wp_query;
        $wp_query = null;
        $wp_query = new WP_Query('posts_per_page=12&paged=' . $paged);
        if ($wp_query->have_posts()) :
          while ($wp_query->have_posts()) : $wp_query->the_post();
            echo the_title();
          endwhile; ?>
          <nav>
            <?php
            the_posts_pagination(array(
              'mid_size'  => 2,
              'prev_text' => __('Back', 'textdomain'),
              'next_text' => __('Onward', 'textdomain'),
            ));
            ?>
          </nav>
        <?php
          $wp_query = null;
          $wp_query = $temp;
          wp_reset_postdata();
        endif;
        ?>
      </div>
like image 29
OctaviaLo Avatar answered Oct 01 '22 03:10

OctaviaLo