Answer the question
In order to leave comments, you need to log in
Checking in the category.php file, how to display a certain number of category posts?
This is how I display the first 3 posts of the category
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('category_name' => 'stories-of-successful', 'posts_per_page' => 3, 'paged' => $paged );
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
If the category name is not specified, then 3 posts are always displayed, and does not check if there is only one post in the category. You can make a bunch of cycles for a specific category, but in my opinion this is nonsense ...
how do I get the id of the category (clicked),
Answer the question
In order to leave comments, you need to log in
No need to bother with custom loops. There is a pre_get_posts hook for this purpose. In functions.php:
function your_function_name( $query ) {
if( $query->is_main_query() && $query->is_category( 'your_category' ) ) {
$query->set( 'posts_per_page' => 3 );
}
}
add_action( 'pre_get_posts', 'your_function_name' );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question