Pagination for a different number of posts Wordpress open

Pagination for a different number of posts Wordpress

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators

The number of posts on the page is regulated by settings in the admin panel of the site.

The number of posts displayed can be edited in the admin panel
The number of posts displayed can be edited in the admin panel

But how to turn on navigation for different numbers of posts on different pages? Let’s find out!

Your post are displayed on the page using this or a similar cycle. The data in the cycle is taken from the general WP_Query
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'entry' ); ?>

<?php echo paginate_links() ?> //navigation function

<?php endwhile; endif; ?>

To change the number of post on one page and enable pagination, we need to create a new WP_Query and transfer the number of posts to for output.

<?php

$paged = '';

if(is_singular()) {
  $paged = (get_query_var('page')) ? get_query_var('page') : 1; 
}else{
  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
}

$newnumberofposts = new WP_Query(array(
'post_type'=>'post', 
'post_per_page'=>5, //number of entries per page
'paged' => $paged
)
);
?>

<?php if ( $newnumberofposts->have_posts() ) : while ( $newnumberofposts->have_posts() ) : $newnumberofposts->the_post(); ?>
<?php get_template_part( 'entry' ); ?>

<!-- Pagination -->
<?php 
$bignumber = 9999999999; //

echo paginate_links(array(
    'base' => str_replace($bignumber, '%#%', esc_url(get_pagenum_link($bignumber))),
    'format' => '?paged=%#%',
    'current' => max(1, get_query_var('paged')),
    'total' => $newnumberofposts->max_num_pages
));
?>

<?php endwhile; endif; wp_reset_postdata(); ?>

0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation