How to include simple pagination in a Wordpress Custom Post Type Query

  • 2 years-11 months

    How to include simple pagination in a Wordpress Custom Post Type Query

    • Category : wordpress
    • Viewed : 39 times
    • Posted On :
    • Posted by : Mohit

    For a WordPress blog, pagination alludes to showing a predetermined number of posts per page. Pagination permits clients the capacity to explore substantially less demanding and more profound into the chronicles. In this post, I will demonstrate that my past instructional exercise about including Pagination without a module in WordPress, can likewise be utilized on Custom Post Types with no alterations. I will likewise demonstrate the means I took to test my past instructional exercise's pagination code and demonstrate that it is as yet substantial for custom post writes.

    <?php
    $paged = (get_query_var('page')) ? get_query_var('page') : 1;          
    $args = array( 'paged'=> $paged,'post_type' => 'product','orderby' => 'post_date','order'=> 'ASC','posts_per_page' => 10,'post_status'=>'publish');        
    
    $query = new WP_Query($args);
    if ($query->have_posts()) : 
    while ($query->have_posts()) : $query->the_post();
        $id=get_the_ID();
    ?> 
          
    <a href="<?php the_permalink() ?>" class="post-title">
        <h2><?php the_title();?></h2>
    </a>
    <div class="post-content"> <?php echo '<p>'.substr(get_the_content(),0,190).'...</p>'; ?> </div>
    
    <?php
    endwhile; endif;
    ?>
     <?php
     echo '<div class="blogPager">';
     echo '<ul class="pagination"> ';
     if( $paged-1 >= 1){
        echo ' <li class="'.($i == $paged ? 'active ' : '').'page button"><a   href="'.get_pagenum_link(($paged-1 > 0 ? $paged-1 : 1)).'"  > Previous</a></li>';
     }
    
    if($query->max_num_pages > 10) {
        for($i=1;$i<=$query->max_num_pages;$i++){
            echo '<li class="'.($i == $paged ? 'active ' : '').'page button"><a  href="'.
           get_pagenum_link($i).'">'.$i.'</a></li>';
    
        }
    }
    if( $query->max_num_pages > $paged  ) {
        echo '<li class="'.($i == $paged ? 'active ' : '').'page button"> <a   href="'.get_pagenum_link(($paged+1 <= $query->max_num_pages ? $paged+1 :
                $query->max_num_pages)).'">Next </a></li>';
    }
    echo '</ul> ';
    echo '</div> ';
    wp_reset_postdata();
    ?>
     

Report this or Ask your own question

Your Answer

When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.