I am trying to get this template to work.
1 Answer
You should never create your own custom WP_Query
queries on category templates (or any other archive template) and use them as main loop.
Why? Because it will break pagination. WordPress will still create all pagination links for such archives based on default query.
So if you want to modify the query in such way, that is should get all posts ordered by date, then you should use pre_get_posts
action (add this to functions.php
file):
function show_all_posts_on_category_archives( $query ) {
if ( ! is_admin() && is_category() && $query->is_main_query() ) {
$query->set( 'posts_per_page', -1 );
$query->set( 'orderby', 'date' ); // you shouldn't need this one - date is default value
}
}
add_action( 'pre_get_posts', 'show_all_posts_on_category_archives' );
And after using that code you can use typical loop in your template file:
while ( have_posts() ) : the_post();
...
endwhile;
-
Yes, after adding it to functions.php you should see all posts on category archive. Commented Jan 28, 2019 at 12:44
-
It's because the action above is checking for the category archive to modify only queries for this template. If you want to modify other query, you'll have to target other page with the condition inside your action. Commented Jan 28, 2019 at 13:01
pre_get_posts
action to do it.