$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to get links to previous and next archive page based on tag|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to get links to previous and next archive page based on tag

matteradmin11PV0评论

I use Extra theme from Elegant themes. One of specific functions of this theme is category layout builder, which creates archives posts based on taxonomy.

I have created such layout pages based on tags (that page only posts only with some certain tag are shown). There will be more than 20 similar pages, just with posts based on different tag. Now I want to insert a shortcode to show links to these previous and next archive pages (get previous and next archive pages) based on tags.

Current tag I get with code:

$tag = get_the_tags();

if ($tags){
    foreach( $tags as $tag ) :
        echo("<script>console.log('PHP: ". $tag->slug ."');</script>");
    endforeach;
}

a whole tag list I get with:

$all_tags = get_tags(array('get'=>'all'));

if($all_tags) {
     foreach ($all_tags as $listtag):
         echo("<script>console.log('PHP: ". $listtag->slug  ."');</script>");
    endforeach;
} else {
    _e('No tags created.', 'text-domain');
} 

How could I get previous and next tags? Also, I want to get last tag of tag array if I am already on first tag and looking for previous tag (accordingly to get first tag when I am on the last tag and looking for next tag).

I understand I should retrieve a whole tag array and then get position of current tag in that tag array. Then to get tag with 'current tag position - 1' and 'current tag position + 1'. I have searched for that, tried to implement idea of similar solutions based on categories (like How to get next previous category in same taxonomy?, Is it possible to put next and previous category links? etc) with no luck.

I am based on javascript (that you see from my code to insert console), so my questions might sound a bit crazy.

I use Extra theme from Elegant themes. One of specific functions of this theme is category layout builder, which creates archives posts based on taxonomy.

I have created such layout pages based on tags (that page only posts only with some certain tag are shown). There will be more than 20 similar pages, just with posts based on different tag. Now I want to insert a shortcode to show links to these previous and next archive pages (get previous and next archive pages) based on tags.

Current tag I get with code:

$tag = get_the_tags();

if ($tags){
    foreach( $tags as $tag ) :
        echo("<script>console.log('PHP: ". $tag->slug ."');</script>");
    endforeach;
}

a whole tag list I get with:

$all_tags = get_tags(array('get'=>'all'));

if($all_tags) {
     foreach ($all_tags as $listtag):
         echo("<script>console.log('PHP: ". $listtag->slug  ."');</script>");
    endforeach;
} else {
    _e('No tags created.', 'text-domain');
} 

How could I get previous and next tags? Also, I want to get last tag of tag array if I am already on first tag and looking for previous tag (accordingly to get first tag when I am on the last tag and looking for next tag).

I understand I should retrieve a whole tag array and then get position of current tag in that tag array. Then to get tag with 'current tag position - 1' and 'current tag position + 1'. I have searched for that, tried to implement idea of similar solutions based on categories (like How to get next previous category in same taxonomy?, Is it possible to put next and previous category links? etc) with no luck.

I am based on javascript (that you see from my code to insert console), so my questions might sound a bit crazy.

Share Improve this question edited Mar 9, 2019 at 11:15 Skaidrius asked Mar 9, 2019 at 10:22 SkaidriusSkaidrius 195 bronze badges 2
  • What is your question? Or you want to hire a programmer for free? – Max Yudin Commented Mar 10, 2019 at 11:23
  • @MaxYudin have you read my question? At least a title? – Skaidrius Commented Mar 11, 2019 at 8:37
Add a comment  | 

1 Answer 1

Reset to default 1

Ok, thank you all who wanted to help. My final code (based on https://goo.gl/c55S3v and it is working perfectly):

$this_tag = get_queried_object();
$all_tags = get_tags();

foreach( $all_tags as $position => $tag ) :
    if( $this_tag->term_id == $tag->term_id ) :
        $next_tag_pos = $position + 1;
        $prev_tag_pos = $position - 1;
        break;
    endif;
endforeach;

$prev_tag_pos = $prev_tag_pos < 0 ? count($all_tags) - 1 : $prev_tag_pos;
$next_tag_pos = $next_tag_pos > count($all_tags) - 1 ? 0 : $next_tag_pos;

echo("<script>console.log('Previous tag: ". $prev_tag_pos . " ". get_term_link( $all_tags[$prev_tag_pos] ) . "');</script>");
echo("<script>console.log('Next tag: ". $next_tag_pos . " ". get_term_link( $all_tags[$next_tag_pos] ) .  "');</script>");
Post a comment

comment list (0)

  1. No comments so far