How to make the link to the category for a post go to corresponding page number in archive?

For instance, I am at http://url.com/single-post and in the post there is a link to the category that navigates to http://url.com/category/page/3/ which is the corresponding archive page that contains the current post.

I’m trying to pass a global variable from archive.php to single.php but it is not working:

Read More
//archive.php 
<?php
echo '$page: '.$page.'<br />';
global $postCatPagenum;
$postCatPagenum = $page;
echo '$postCatPagenum: '.$postCatPagenum;
?>

//single.php
<?php
global $postCatPagenum;
echo '$postCatPagenum: '.$postCatPagenum;
?>

Eventually I would like to make a function like this to run in single.php:

// Persistent category page
function persistant_cat_page() {
  global $postCatPagenum;
  $category = get_the_category(); 
  if (!$postCatPagenum){
      $pageUrl = '';
  } else {
      $pageUrl = '/page/'.$postCatPagenum;
  }
  if($category[0]){
      echo '<a href="'.get_category_link($category[0]->term_id).$pageUrl.'">'.$category[0]->cat_name.'</a>';
  }
}

Thanks!

Related posts

Leave a Reply