0

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.

9
  • 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, 2012 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, 2012 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, 2012 at 16:12
  • There's a get_next_post function, but nothing similar for category.
    – Liana Mir
    Sep 18, 2012 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, 2012 at 16:50

3 Answers 3

4

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] );
1
  • 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, 2012 at 3:10
0

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
);
0

This function works for categories (without hierarchy) and tags. It also reaches the first taxonomy (cat or tag). You can add it to your functions.php

<?php
if (!function_exists('my_tax_paging_nav')) {
/*
Display navigation to next/previous set of categories/tags when applicable.
*/
function my_tax_paging_nav()
{
    $this_taxonomy = get_queried_object();
    if (is_category()) {
        $taxonomies = get_categories();
        //for other languages than english
        $plural     = '';
        $taxlabel   = 'Category';
    }
    if (is_tag()) {
        $taxonomies = get_tags();
        //for other languages than english
        $plural     = 's';
        $taxlabel   = 'Tag';
    }

    foreach ($taxonomies as $position => $tax):
        if ($this_taxonomy->term_id == $tax->term_id):
            $next_tax = $position + 1;
            $prev_tax = $position - 1;
            break;
        endif;
    endforeach;

    //variables to show/hide when top/bottom is reached
    $showPrev;
    $showNext;
    //bottom
    if ($prev_tax < 0){
        $prev_tax=count($taxonomies) - 1;
        $showPrev='';
        }else{
            $prev_tax;
            $showPrev = '<span class="meta-nav">&larr; </span><small>Vorherige' . $grammatik . ' ' . $taxlabel . ': </small>' . $taxonomies[$prev_tax]->name;
        }

    //top
    if ($prev_tax == count($taxonomies)-2){
        $next_tax=0;
        $showNext = '';
        }else{
            $showNext = '<small>Nächste' . $grammatik . ' ' . $taxlabel . ': </small>' . $taxonomies[$next_tax]->name.'<span class="meta-nav"> &rarr;</span>';
            }

    $prevLink = get_term_link( $taxonomies[$prev_tax] );
    $nextLink = get_term_link( $taxonomies[$next_tax] );
    ?>
    <nav class="navigation post-navigation" role="navigation">
        <div class="nav-links">
            <div class="nav-previous"><a href="<?php echo $prevLink;?>"><?php echo $showPrev; ?></a></div>
            <div class="nav-next"><a href="<?php echo $nextLink;?>"><?php echo $showNext ?></a></div>
        </div><!-- .nav-links -->
    </nav><!-- .navigation -->
    <?php
  }
}//end if     

You can call this function in category.php or tag.php:

<?php my_tax_paging_nav(); ?>

You may want to change the html markup to your needs. I hope this may help someone. regards theo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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