Loop for Postcards with Sorting

I've used the following loop in the category.php file: <?php $args = array( 'post_type' => 'post', 'posts_per_page' => get_option('posts_per_page'), 'paged' => get_query_var('paged'), 'suppress_filters' => true, 'orderby' => 'date', 'order' => 'ASC', ); $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <?php get_template_part( 'templates/archive-category-tag/archive_c-t-s_Card_theme' ); ?> <?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php endif; ?> The loop above correctly displays the postcards for me, and as you can see in its arguments: 'posts_per_page' => get_option('posts_per_page'), I have used Based on this, the number of display postcards can be controlled through the WordPress settings. (I have specified the number of 10 postcards on the page). So far everything is working fine but the loop above has a major problem! For example: If my category contains 1 or 2 or 3 posts; The number of display postcards on the page should be the same, but it is not like this in the output, and the number of output postcards is equal to 10 postcards!? For this purpose, I added the following line to the above iteration loop code: if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); as: if ( $the_query->have_posts() ) : while (have_posts() ) :the_post(); I changed; And now the number of displayed postcards on the page is completely consistent with the category, but there is no news of sorting the postcards!!! As a last resort I created a condition to first detect the number of posts connected to a category and then display postcards based on the number of posts. The code I have written so far is as follows: <?php $args = array( 'post_type' => 'post', 'posts_per_page' => get_option('posts_per_page'), 'paged' => get_query_var('paged'), 'suppress_filters' => true, 'orderby' => 'date', 'order' => 'ASC', ); $the_query = new WP_Query( $args ); // Check the total number of posts in the current category $total_posts_in_category = $the_query->found_posts; // If there are posts in the category if ( $the_query->have_posts() ) : // Loop through the posts as many times as there are posts while ( $the_query->have_posts() && $total_posts_in_category > 0 ) : $the_query->the_post(); get_template_part( 'templates/archive-category-tag/archive_c-t-s_Card_theme' ); // Decrease the number of remaining posts $total_posts_in_category--; endwhile; wp_reset_postdata(); endif; ?> This code, like the repeating loop that I put at the beginning of my question, only works well for sorting the posts and it is not able to display the postcards according to the category!? I have been dealing with this problem for a few days and I have tried many repetition loops that did not work! I don't see a way anymore! Is there any way to solve this problem? Thanks in advance for any help.

Comment (0)

You’ll be in good company