0

I wish create a next previous category navigation. I found an old post on stackexchange (2012) about category navigation but it's returning an error.

So I made something like this:

$category = get_the_category();
$name= $category[0]->name;
$slug= $category[0]->slug;
$id = $category[0]->cat_ID;

$previouscat = $id - 1;
$nextcat = $id + 1;
$category_link_next = get_category_link( $nextcat );
$category_link_previous = get_category_link( $previouscat );
?>

<a href="<?php echo $category_link_previous ?>" class="btnSuivPrec btnPrec"><span class="icon-precedent"></span></a>
<div class="picto colo<?php echo $slug ?>">
<span class="icon-<?php echo $slug ?>"></span>
</div>
<a href="<?php echo $category_link_next ?>" class="btnSuivPrec btnSuiv"><span class="icon-suivant"></span></a>

But how to modify the code to have an infinite loop ?

  • Can you elaborate on exactly what you're trying to do. Do you want the next and previous page of posts within a category? Do you want to link to the posts for the 'next' category from a category archive? – Jacob Peattie May 17 '18 at 8:17
  • just a link to next and previous category archive – Lust May 17 '18 at 9:14
0

This is an extremely unreliable way to determine the next and previous category IDs:

$previouscat = $id - 1;
$nextcat = $id + 1;

There's absolutely zero guarantee that the IDs of adjacent categories are only 1 apart. In fact the only time that would be the case is if all the categories were added at once, and no other tags or custom terms (like product categories) were ever added. Was the answer you got that from upvoted or accepted? I'd be concerned if it was.

Anyway, the fact is that Categories don't really have an order. They're just sorted by when they were inserted into the database and it doesn't suit WordPress' conception of Categories to imagine them having a fixed order.

That being said if you look at a list of categories they'll be in a particular order and I guess in some cases you'll want to get the adjacent categories for another category.

So the proper way to do that is to first get a list of all categories, then find the position of the current category in the resulting list. Then you can use +1 and -1 to get the next and previous categories based on position in the list because the position will always increment by one. Then you can use the categories in those positions for the links.

// Make sure the code only runs on category archives.
if ( is_category() ) {
    // Initialise variables to put the links into.
    $previous_link = '';
    $next_link = '';

    // Get an array of all category IDs.
    $category_ids = get_categories( ['fields' => 'ids'] );

    // Find the current category's position in the list of all categories.
    $position = array_search( get_queried_object_id(), $category_ids );

    // If there is a category in the list before the current category.
    if ( isset( $category_ids[$position-1] ) ) {
        // Set its link to the $previous_link variable.
        $previous_link =  get_category_link( $category_ids[$position-1] );
    }

    // If there is a category in the list after the current category.
    if ( isset( $category_ids[$position+1] ) ) {
        // Set its link to the $next_link variable.
        $next_link = get_category_link( $category_ids[$position+1] );
    }

    // Now you can output the links however you'd like.
    echo $previous_link;
    echo $next_link;
}
  • thank you, it's working for the next and previous, but how to make it infinite ? If I have this for exemple: Cat A - CAT B - CAT C - CAT D the CAT D "$next_link" display the current cat (because it's the last of the list I think), but I want "$next_link" in CAT D display CAT A link. So loop infinite – Lust May 17 '18 at 12:47

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.