Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm not trying to navigate to the next post within a category, but to put previous category and next category under the category description on its archive page and navigate between archive pages.

I've searched, but I'm not sure this is even possible without hardcoding categories, and I don't want to do that. I'd like to dynamically link.

Not asking anyone to write any code for me, just asking does this sound like a good starting direction for what I need to look at doing:

Build a get_next_category function by using the wp_list_categories, then pulling the adjacent from that list and turning it into a link?

In short, I want a category archive page to show a category title, description (know how to do both of those), then next and previous category links before it shows the posts. I am not creating a list of categories, just navigation links.

Hope that clarifies what I'm trying to do. Still searching codex and exchange.

share|improve this question
1  
Please search harder and show us the code you've tried so far and what exactly didn't work. There're dozens of Q/As about this topic already. –  kaiser Sep 18 '12 at 7:57
    
There's not a single one that's asking how to go to the next category, only the next posts, but I'll scour the codex again for a starting point. I'm not finding even any functions to TRY. –  Liana Mir Sep 18 '12 at 16:09
    
Maybe you can explain what exactly makes a category "the next/previous category". By default they have no hierarchy/order. –  kaiser Sep 18 '12 at 16:12
    
There's a get_next_post function, but nothing similar for category. –  Liana Mir Sep 18 '12 at 16:19
1  
Scratch "I figured by ID order." Not solved. I edited the question to clarify and am continuing to research myself. –  Liana Mir Sep 18 '12 at 16:50
show 4 more comments

2 Answers

up vote 0 down vote accepted

Not sure if this is what you want, gets all the categories and outputs a link to the next and previous in the order returned from get_categories():

$this_category = get_queried_object();
$categories = get_categories();

foreach( $categories as $position => $cat ) :
    if( $this_category->term_id == $cat->term_id ) :
        $next_cat = $position + 1;
        $prev_cat = $position - 1;
        break;
    endif;
endforeach;

$next_cat = $next_cat == count($categories) ? 0 : $next_cat;
$prev_cat = $prev_cat < 0 ? count($categories) - 1 : $prev_cat;

echo 'previous: ' . get_term_link( $categories[$prev_cat] );
echo 'next: ' . get_term_link( $categories[$next_cat] );
share|improve this answer
    
Thank you. Yes, this is what I was looking for. –  Liana Mir Sep 18 '12 at 18:22
    
Edited the code like so: echo '<div class="nav-previous"><a href="'.get_term_link( $categories[$prev_cat] ).'">Previous Category</a></div>'; echo '<div class="nav-next"><a href="'.get_term_link( $categories[$next_cat] ).'">Next Category</a></div>'; echo '<br><br>'; –  Liana Mir Sep 19 '12 at 3:10
add comment

As often, the answer depends on your

$permalinks = get_option( 'permalink_structure' )

If this doesn't return false, then you've permalinks enabled. You can then check your settings and build your exact links from it.

You then can simply extract those list/object parts, that you need and build your links.

// Maybe you want them for the labels?
$cat_names = wp_list_pluck( get_the_category( get_the_ID() ), 'name' );

// For nice permalinks
$cat_slugs = wp_list_pluck( get_the_category( get_the_ID() ), 'slug' );
// For non-beautiful permalink sites
$cat_IDs   = wp_list_pluck( get_the_category( get_the_ID() ), 'term_id' );

As I don't know how you're going to sort/order them and what exactly your goal is, I can't offer you a solution, but simply would have to determine what your next/prev cat is and then build the links:

printf( 
     '<a href="%1$s" title="%2$s">%2$s</a>'
    ,home_url( '/' ) // Append your link inside the single quotes after the slash
    ,$cat_name[0]    // Set your next/prev cat here
);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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