I use the following code to create a repetition loop to display my post cards in the category.php
file:
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<?php get_template_part('templates/archive-category-tag/card_cat-tag-archive_theme'); ?>
<?php endwhile; endif; ?>
The above repetition loop correctly displays my post cards with the number I have specified in the WordPress settings. (I have set the number of displayed posts to 10).
Now I want to be able to sort these post cards in addition to displaying them.
For this purpose, I edited the repetition loop code as follows:
<?php
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC'
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while(have_posts()) : the_post();
get_template_part('templates/archive-category-tag/card_cat-tag-archive_sorting_theme');
endwhile; endif; ?>
With this method, my post cards are still displayed correctly, but I cannot sort them from oldest to newest or newest to oldest.
Note:
The reason for using such a repetition loop is that if my category has one post, only one post card should be displayed on the page, not ten.
And this is only possible with the default repetition loop:
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
And I don’t know of any other way.
So the reason for creating such a repetition loop is to display the post cards on the page according to the category and the posts connected to it.
In summary:
I want to be able to sort the post cards in addition to displaying them, provided that the number of displayed post cards matches the number of posts connected to the category.