0

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.

  • What is your question? Or you want to hire a programmer for free? – Max Yudin Mar 10 at 11:23
  • @MaxYudin have you read my question? At least a title? – Skaidrius Mar 11 at 8:37
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>");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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