CPT : Next or Previous term link when has not adjacent post

My structure is like this :

Book name (cpt)
--- chapters (taxonomy)
-------- chapter 1 (term)
-------- Chapter 2 (term etc)

I have some posts into book added in terms I wish in single-book.php add next and previous links but on the last post (so if there is not next post) display the link to chapter 2 to continue reading for the user.

I don't know how to do this...

This is my code :

div class=previous-post-link position-absolute
        ?php 
            global $post;
            $postID = get_the_ID();
            $post_type = get_post_type( get_the_ID() );
            $taxonomies = get_object_taxonomies($post_type);
            foreach ($taxonomies as $taxonomy) {        
                $taxo = $taxonomy;
            }
            $term_list = wp_get_post_terms( $postID, $taxo, array( 'fields' = 'all' ) );
                            
            foreach ($term_list as $termCurent){ 
                $myterm =  $termCurent ?
            }

        div class=previous
            ?php $prev_post = get_adjacent_post( true, '', true, $taxo ); 
                if ( is_a( $prev_post, 'WP_Post' ) ) { ?
                a href=?php echo get_permalink( $prev_post-ID ); ??php echo get_the_title( $prev_post-ID ); ?i class=ico-prev/i/a

            ?php } else{ 
                wp_reset_query(); ?
                
                        a href=?php echo get_permalink( $myterm-ID ); ??php echo $myterm-name; ?i class=ico-prev/i/a          
                
                ?php }?
        /div

        div class=next
        ?php 
            $next_post = get_adjacent_post( true, '', false, $taxo); 
            if ( is_a( $next_post, 'WP_Post' ) ) {  ?
            a href=?php echo get_permalink( $next_post-ID ); ??php echo get_the_title( $next_post-ID ); ?i class=ico-prev/i/a
            ?php } else{
                // show the next term link
            }
        ?             
        /div
    /div 

If somebody can help, it would be great :)

Thanks

Topic next-post-link terms taxonomy custom-post-types Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.