The problem here is that you’re not using the function correctly – it returns an object, and won’t output anything by itself. You also need to ensure that $slug
and your other arguments are set & have values, otherwise you’ll just trigger an error.
Personally, I’d go one step further and use the following function instead – the arguments are more flexible, and there’s simply no need for custom SQL queries; you can capitalise on existing WordPress functions with the added benefit of object caching.
/**
* Get adjacent category or term.
*
* @link http://wordpress.stackexchange.com/q/137203/1685
*
* @param string $direction "next" or "previous"
* @param mixed $term Term ID, slug or object. Defaults to current tax archive, if applicable.
* @param string $taxonomy Optional. Not required if $term is object.
* @return object|null
*/
function get_adjacent_term( $direction = 'next', $term = null, $taxonomy = null ) {
if ( $term ) {
// Bit of type checking as we want the term object.
if ( is_numeric( $term ) )
$term = get_term( $term, $taxonomy );
elseif ( ! is_object( $term ) )
$term = get_term_by( 'slug', $term, $taxonomy );
} elseif ( is_category() || is_tag() || is_tax() ) {
// Default to current term object if $term was omitted.
$term = get_queried_object();
}
if ( ! $term || is_wp_error( $term ) )
return null;
// Get all siblings of term.
$terms = get_terms(
$term->taxonomy,
array(
'parent' => $term->parent,
)
);
// Find index of current term in stack.
foreach ( $terms as $index => $_term ) {
if ( $_term->term_id == $term->term_id )
break;
}
if ( $type === 'next' ) {
if ( ! isset( $terms[ ++$index ] ) )
return null;
} elseif ( ! isset( $terms[ --$index ] ) ) {
return null;
}
// $index will now be the adjacent term.
return $terms[ $index ];
}
And in use, at least on taxonomy archive pages, is simply:
<?php if ( $next = get_adjacent_term() ) : ?>
<div style="float: right;">
See <a href="https://wordpress.stackexchange.com/questions/137203/<?php echo get_term_link( $next ) ?>"><?php echo $next->name ?></a>
</div>
<?php endif ?>