简体   繁体   中英

Pagination not working on custom Wordpress loop

I have been working on creating a custom loop and setting how many posts per page. I needed to make this loop separate to the blog posts per page - hence the custom loop.

Now the loop does work, however I noticed it is conflicting with the pagniation... pagination is showing but pages are not changing when you click 'next'.

Here is my loop:

<?php 
     $args = array('posts_per_page'=>12, 'post_type' =>'office'); 
     $category_posts = new WP_Query($args); 

    if($category_posts->have_posts()) :  
    while($category_posts->have_posts()) : 
    $category_posts->the_post();
?>

    <div class="office-item-wrapper">

        <div class="office-item">

            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>


            </article>

        </div>

    </div>

<?php endwhile; ?>

<?php else: ?>

    <article>
        <h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
    </article><!--/ Article -->

<?php endif; ?>

Thanks in advance to anyone who might be able to help me solve this issue :-)

In WP, the pagination is sent by a GET variable under page , just add it to the query args:

$page = get_query_var('paged');
$args = array('posts_per_page'=>12, 'post_type' =>'office', 'paged'=>$page);  

I recommend you to use get_query_var('paged') because if it's empty it will be send you to the first page.

Note: In some themes, the page number is sent in the page key, so try with page or paged .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM