最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Why is this category page limiting the number of posts?

programmeradmin6浏览0评论

I am developing my first Wordpress theme. I've created a custom page for a specific post category, and I would like to display all the posts from this specific category on the page.

The code is largely borrowed from elsewhere, so it's highly likely that it's not best practice, but it seems to be working. However, there is one fairly significant problem:

The code I'm using limits the number of posts to the first ten (sorted by alphabetical order). Could anyone tell me how I can change the code I've written so that all of the posts in this category will be displayed?

Any help would be very greatly appreciated!

<?php
  $r = new WP_Query(
    apply_filters(
       'widget_posts_args',
         array(
           'post_status'          => 'publish',
           'cat'                   => 5,
           'orderby'               => 'title',
           'order'                 => 'ASC',
         ),
       $instance
    )
  );

  if ( ! $r->have_posts() ) {
    return;
  }
?>
 <ul>
   <?php foreach ( $r->posts as $hof_post ) : ?>
   <?php
     $post_title   = get_the_title( $hof_post->ID );
     $title        = ( ! empty( $post_title ) ) ? $post_title : __( '(no title)' );
     $thumbnail         = get_the_post_thumbnail($hof_post->ID);
     $excerpt           = get_the_excerpt($hof_post->ID);
     $aria_current = '';
   ?>
   <li>
    <a href="<?php the_permalink( $hof_post->ID ); ?>">
      <?php echo $thumbnail; ?>
      <h3><?php echo $post_title ?></h3>
      <p><?php echo $excerpt ?></p>
    </a>
  </li>
  <?php endforeach; ?>
</ul>

I am developing my first Wordpress theme. I've created a custom page for a specific post category, and I would like to display all the posts from this specific category on the page.

The code is largely borrowed from elsewhere, so it's highly likely that it's not best practice, but it seems to be working. However, there is one fairly significant problem:

The code I'm using limits the number of posts to the first ten (sorted by alphabetical order). Could anyone tell me how I can change the code I've written so that all of the posts in this category will be displayed?

Any help would be very greatly appreciated!

<?php
  $r = new WP_Query(
    apply_filters(
       'widget_posts_args',
         array(
           'post_status'          => 'publish',
           'cat'                   => 5,
           'orderby'               => 'title',
           'order'                 => 'ASC',
         ),
       $instance
    )
  );

  if ( ! $r->have_posts() ) {
    return;
  }
?>
 <ul>
   <?php foreach ( $r->posts as $hof_post ) : ?>
   <?php
     $post_title   = get_the_title( $hof_post->ID );
     $title        = ( ! empty( $post_title ) ) ? $post_title : __( '(no title)' );
     $thumbnail         = get_the_post_thumbnail($hof_post->ID);
     $excerpt           = get_the_excerpt($hof_post->ID);
     $aria_current = '';
   ?>
   <li>
    <a href="<?php the_permalink( $hof_post->ID ); ?>">
      <?php echo $thumbnail; ?>
      <h3><?php echo $post_title ?></h3>
      <p><?php echo $excerpt ?></p>
    </a>
  </li>
  <?php endforeach; ?>
</ul>
Share Improve this question asked Jul 17, 2020 at 16:38 Tom DaviesTom Davies 232 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

10 is the default page length which is taken from the Wordpress settings.

It's a little bit hidden but there in the docs for WP Query there's a parameter posts_per_page which can be set to -1 to get all posts, so you just need to add that to the arguments to pass to WP_Query.

(If that doesn't work and you copied that WP_Query code from somewhere, you might need to remove the apply_filters() part and just keep the inner array() and pass that directly to WP_Query)

发布评论

评论列表(0)

  1. 暂无评论