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:
//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!