0

I made a custom php page for a category, and want to restrict the max limit of posts per page to 5, and have next and prev buttons. Can someone help?

<?php 
/**
* Video Template
*/

get_header(); ?> 

<section id="primary" class="site-content">
<div id="content" role="main">

<?php 
// Check if there are any posts to display
if ( have_posts() ) : ?>

<header class="archive-header">
<h1 class="archive-title">Videos list<?php single_cat_title( '', false ); ?>
</h1>


<?php
// Display optional category description
 if ( category_description() ) : ?>
<div class="archive-meta"><?php echo category_description(); ?></div>
<?php endif; ?>
</header>

<?php

// The Loop
while ( have_posts() ) : the_post(); ?>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link 
to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<div class="entry">
<small><?php the_time('F jS, Y') ?></small>
<?php the_content(); ?>

</div>

<?php endwhile; 

else: ?>
<p>Sorry, no posts matched your criteria.</p>


<?php endif; ?>
</div>
</section>



<?php get_footer(); ?>
1

to change the number of 'posts_per_page' for one specific category archive page (assuming your are using a different number for all other archives), try working with the 'pre_get_posts' action; see https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

assuming that your category template is for the 'video' category, the code to be added into functions.php of your (child) theme, could be, for example:

function video_category_pagesize( $query ) {
    if ( is_admin() || ! $query->is_main_query() )
        return;

    if ( is_category( 'video' ) ) {
        // Display 5 posts per page for the 'video' category archive
        $query->set( 'posts_per_page', 5 );
        return;
    }

}
add_action( 'pre_get_posts', 'video_category_pagesize' );

for the next prev buttons, for example, consider to use https://codex.wordpress.org/Function_Reference/next_posts_link and https://codex.wordpress.org/Function_Reference/previous_posts_link and review https://codex.wordpress.org/Pagination

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.