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>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0