简体   繁体   中英

Custom Post and Pagination in WordPress

I know there is plenty of information out there on pagination and custom post types, but after doing the research and trying a few different methods, I'm just not having any luck.

Would greatly appreciate any thoughts of why the code below isn't working:

<!-- main content -->
<main class="main-content">

  <h1><?php the_title(); ?></h1>

  <div class="projects">

    <?php
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

      $args = array(
      'numberposts' => -1,
      'post_type' => 'projects',
      'meta_key' => 'project-status',
      'meta_value' => 'Completed Project',
      'paged' => $paged,
      'showposts' => 3
      );

      $project = new WP_Query( $args );
    ?>

    <?php
      if ( $project->have_posts() ) :
      while ( $project->have_posts() ) : $project->the_post();
    ?>

        <?php get_template_part('content-project'); ?>

      <?php endwhile; ?>

    <?php
      wp_pagenavi();
      wp_reset_postdata();
    ?>

    <?php endif; ?>

</div>

</main> 

First of all, you cannot use numberposts and showposts together in the same query

Secondly, numberposts is meant to be used with get_posts . In WP_Query it is an invalid parameter

Thirdly, showposts has been dropped in favor of posts_per_page

For the pagination and how to use wp_pagenavi, have a look at the documentation on the author's website . According to the docs, this should work

wp_pagenavi( array( 'query' => $project ) );

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