0

I have a problem with the pagination on a single.php page.

The idea is to have a list of my created posts (the excert, the title and a link to the post) on the post page (single.php) that shares the same category with the one that I'm viewing.

I have a custom loop that filters the category and the maximum number of posts to show per page, but the next page doesn't exist (even manually writing the: www.example.com/my-post/page/2, /?page=2/ or /2/). It redirects me to www.example.com/my-post/

If I manually change the $page variable, the posts listed change to the next ones that should be shown.

Any idea why the links in the single.php don't work as in a page template?

Here is the code of my custom wp_query and the pagination (I know its a bit awkward, but on my other page Template works perfectly :

the_content(); // The post edited in the WP editor //

$number_of_posts = get_field('number_of_posts');

$arg = array(
'post_type'   => 'post',            
'showposts'   => -1,    
'post_status' => 'any',
'cat'         => $actual_cat_id,    
); 

$count_query = new WP_Query($arg);  
$total_post = $count_query->post_count;
$total_pages = ceil($total_post/$number_of_posts); 

if (get_query_var('paged') == null) {   
$page= 1;                           
} else {
$page = get_query_var('paged');     
}
                                        
$arguments = array(     
 'post_type'    => 'post',  
 'post__not_in' => array($actual_post_id),  
 'showposts'    => $number_of_posts,            
 'paged'       => $page,    
 'orderby'     => 'date',               
 'order'       => 'DESC',                   
 'post_status'  => 'any',                   
 'cat'         => $actual_cat_id,   
);

$paged = $page;

$my_query = new WP_Query($arguments);

while...

// THE LOOP (with the excert and the title for each post) //

...endwhile

wp_reset_postdata();

// PGINATION WITH ARROWS //
if ($total_pages == 1) { 

 <div>
  <span> &lt;&lt; </span>
  <span> &lt; </span>
  <span> PAG <?php echo $paged."/". $total_pages; ?> </span>
  <span> &gt; </span>
  <span> &gt;&gt; </span>
 </div>

 } elseif ($paged == 1) {
  $paged_siguiente = $paged+1; 
  <div>
    <span> &lt;&lt; </span>
    <span> &lt; </span>
    <span> PAG <?php echo $paged."/". $total_pages; ?> </span>
    <a href="?paged=<?php echo $paged_siguiente; ?>"> <span> &gt; </span> </a>
    <a href="?paged=<?php echo $total_pages; ?>"> <span> &gt;&gt; </span> </a> 
  </div>

 } elseif ($paged>1 && $paged<$total_pages) {
  $paged_siguiente = $paged+1;
  $paged_atras = $paged-1;

   <div>
    <a href="?paged=1"> <span> &lt;&lt; </span> </a>
    <a href="?paged=<?php echo $paged_atras; ?>"> <span> &lt; </span> </a>
    <span> PAG <?php echo $paged."/". $total_pages; ?> </span>
    <a href="?paged=<?php echo $paged_siguiente; ?>"> <span> &gt; </span> </a>
    <a href="?paged=<?php echo $total_pages; ?>"> <span> &gt;&gt; </span> </a>
   </div> 
} elseif ($paged == $total_pages) { 
 $paged_atras = $paged-1; 

 <div>
    <a href="?paged=1"> <span> &lt;&lt; </span> </a>
    <a href="?paged=<?php echo $paged_atras; ?>"> <span> &lt; </span> </a>
    <span> PAG <?php echo $paged."/". $total_pages; ?> </span>
    <span> &gt; </span>
    <span> &gt;&gt; </span>
 </div> 
} 

1 Answer 1

0

Custom loops do not produce pagination like that.

You have two options:

  1. Use custom archive page and provide custom php template for that (i.e. archive-my_name.php), in this custom template you could provide all other necessary content. Then you could use standard built-in pagination functions https://developer.wordpress.org/themes/functionality/pagination/
  2. Continue in the direction you started, with single.php, but you would need to provide your own pagination, together with rewrites for custom pages. Again, https://developer.wordpress.org/themes/functionality/pagination/#methods-for-displaying-pagination-links provides starting point for that.

I advise using option 1. as easier.

3
  • Thx for your advice. I tried the 1st option, but my archive-$posttype.php with the custom post type redirects me to the index.php template. I don't know if I understand your solution/idea
    – Adrian
    Jun 8, 2022 at 14:04
  • It should not redirect to index.php. I now see my mistake, I assumed you are using custom post types, but you are using regular post types. Index.php is the fallback if no other appropriate template is found. I correct myself, you should use archive.php or category.php if you want to have special template for category. You can customize permalinks in dashboards -> settings -> permalinks. And open that page just in case because mere opening of that page flushes permalink rewrite cache, so any incorrect redirects should be fixed automatically.
    – Lovor
    Jun 9, 2022 at 15:14
  • 1
    Thx for your help, but finally this code in my function.php solve the problem: add_filter('redirect_canonical','pif_disable_redirect_canonical'); function pif_disable_redirect_canonical($redirect_url) { if (is_single()) $redirect_url = false; return $redirect_url; }
    – Adrian
    Jun 10, 2022 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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