wp query - Wordpress pagination not returning posts on second page

admin2025-01-07  6

I've got a page where I only want to display one post per page with pagination at the bottom to go to the next/ previous post. This is my code:

  <?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : '1';
$args = array (
    'nopaging'               => false,
    'paged'                  => $paged,
    'posts_per_page'         => '1',
    'post_type'              => 'post',
    'category_name'          => 'enforcement',
);
?>
    <?php 
     $wp_query = new WP_Query($args);
    ?>

    <?php if ( $wp_query->have_posts() ) : ?>
    <?php while ( $wp_query->have_posts() ) : the_post(); ?>   
<!--<div class="case-studies-text-banner">
   <img src=".png"/>
    <div class="case-study-title"><?php the_title(); ?></div>
    <div class="case-study-pdf"><?php if (function_exists("wpptopdfenh_display_icon")) echo wpptopdfenh_display_icon();?></div>
    </div>-->

    <?php the_content(); ?>

    <?php endwhile; ?>

<?php next_posts_link( 'Older Entries »', $query->max_num_pages ); ?>


     <?php wp_reset_postdata(); ?>
    <?php else : ?>
    <p><?php __('No News'); ?></p>
    <?php endif; ?>

It does only show 1 post on the first page but when clicking on older posts and moving to this url www.testing./?cat=8&paged=2 no posts show even though there are 3 posts in this category. Any ideas what I'm doing wrong?

I've got a page where I only want to display one post per page with pagination at the bottom to go to the next/ previous post. This is my code:

  <?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : '1';
$args = array (
    'nopaging'               => false,
    'paged'                  => $paged,
    'posts_per_page'         => '1',
    'post_type'              => 'post',
    'category_name'          => 'enforcement',
);
?>
    <?php 
     $wp_query = new WP_Query($args);
    ?>

    <?php if ( $wp_query->have_posts() ) : ?>
    <?php while ( $wp_query->have_posts() ) : the_post(); ?>   
<!--<div class="case-studies-text-banner">
   <img src="http://www.mariadev.co.uk/wp-content/uploads/2017/05/case-study-layer.png"/>
    <div class="case-study-title"><?php the_title(); ?></div>
    <div class="case-study-pdf"><?php if (function_exists("wpptopdfenh_display_icon")) echo wpptopdfenh_display_icon();?></div>
    </div>-->

    <?php the_content(); ?>

    <?php endwhile; ?>

<?php next_posts_link( 'Older Entries »', $query->max_num_pages ); ?>


     <?php wp_reset_postdata(); ?>
    <?php else : ?>
    <p><?php __('No News'); ?></p>
    <?php endif; ?>

It does only show 1 post on the first page but when clicking on older posts and moving to this url www.testing./?cat=8&paged=2 no posts show even though there are 3 posts in this category. Any ideas what I'm doing wrong?

Share Improve this question edited May 30, 2017 at 17:34 Johansson 15.4k11 gold badges43 silver badges79 bronze badges asked May 30, 2017 at 16:53 tasmanianDevil123tasmanianDevil123 11 bronze badge 1
  • Hi Tasmanian, check this wordpress.stackexchange.com/questions/254199/… and one more think you just save your permalinks. Here is official wordpress next post link developer.wordpress.org/reference/functions/next_posts_link – Irfan Commented Apr 6, 2023 at 5:34
Add a comment  | 

2 Answers 2

Reset to default 0

Try this as your $paged init

<?php global $paged; ?>
<?php 
  if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
  elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
  else { $paged = 1; }  
?>

Please Add this line and Check.

<?php previous_posts_link( 'Newer posts' ); ?>

For More Information Please check this LINK

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736258974a554.html

最新回复(0)