I've been working on creating a link in my header, should say "see enter next category name here"

I've been using this code, found here (I would comment again to ask directly on that question, but I'm not allowed)

    function get_adjacent_category($category_slug,$taxonomy,$type){
global $wpdb;
    $operater=" > ";
    $orderby=" ORDER BY tt.`term_id` ASC ";
    $operater=" < ";
    $orderby=" ORDER BY tt.`term_id` DESC ";
$query="SELECT *,(SELECT `term_id` FROM wp_terms WHERE `slug`='".$category_slug."') AS given_term_id,
    (SELECT parent FROM wp_term_taxonomy WHERE `term_id`=given_term_id) AS parent_id
    FROM  `wp_terms` t
    INNER JOIN `wp_term_taxonomy` tt ON (`t`.`term_id` = `tt`.`term_id`)
    HAVING  tt.taxonomy='".$taxonomy."' AND tt.`parent`=parent_id AND tt.`term_id` $operater given_term_id $orderby LIMIT 1";
return $wpdb->get_row($query);


    $next_category =  get_adjacent_category($slug,$taxonomy,"next");
    $previous_category =  get_adjacent_category($slug,$taxonomy,"previous");

and while I'm not getting an errors, it's not pulling the next category up for me. Just a blank space. Here's the code I'm using to call the function:

    <div style="float:right;">See<?php get_adjacent_category($slug,$taxonomy,"next"); ?></div>

Am I calling it incorrectly? If it helps, I'm working with Thesis theme, and here's the website I'm working with. I'm trying to place it on an archive page, mirroring to the "back to blog" link at the top.

Any insight would be greatly appreciated!!

  • You are not echoing anything, and if you tried you wouldn't get what you expect because get_row() returns an Object by default. These are both basic PHP problems. var_dump() your data from the function and see if it looks right. – s_ha_dum Mar 7 '14 at 15:51

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(
            'parent' => $term->parent,

    // Find index of current term in stack.
    foreach ( $terms as $index => $_term ) {
        if ( $_term->term_id == $term->term_id )

    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="<?php echo get_term_link( $next ) ?>"><?php echo $next->name ?></a>

<?php endif ?>
| improve this answer | |
  • You are a rockstar. That worked perfectly, thank you! – user3158649 Mar 7 '14 at 16:18
  • Quick question, could this work for tags too? I'm a total newb, I thought I knew how to add it in, but apparently did not. – user3158649 Mar 7 '14 at 21:39
  • I have added is_tag() to the function, so yes, will work for them too now :) – TheDeadMedic Mar 8 '14 at 15:50

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.