WordPress/Woocommerce – Add Previous/Next buttons on Category archive-product.php linking to adjecent category lists

I have created my product category template at Woocommerce/archive-product-list.php in my child theme and it is in a list format. I have assigned which woo commerce product categories will use which templates in my functions.php file.

I would like to add Previous & Next Category Link to the template that would dynamically create a url from next and previous category and then link to the adjacent woo commerce category and or child category which would then show the list items from that category.

Read More

I have found very little information on how to do this. Most of the articles are about prev/next post within the same category and that is NOT what I am looking for.

The closest thing that I have found was this from 2012:

$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 '<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>'; 

https://wordpress.stackexchange.com/questions/65450/is-it-possible-to-put-next-and-previous-category-links

I have read some talk about taxonomy and pagination but don’t see how that helps me. Any help would be much appreciated even if it is just pointing me in the right direction.

Thank you.

Related posts

Leave a Reply

1 comment

  1. Below is the code from http://pastebin.com/0GG0HX4x adapted to work with a custom taxonomy (which is what WooCommerce’s product category is).

    I have NOT dug into this to ensure it lists them in the order you’ve described, the logic could get fairly complex to sort out issues around “is this a child? Does this have children? Which taxonomy should display next?” – which could also result in a fair amount more code, and potentially having to change strategies from the one used below. For some ideas of the options available for sorting, please refer to the documentation for get_terms(), particularly the orderby and order section.

    Note also that the code below is untested, so please ask clarifying questions in the comments below and I will do my best to improve the code.

    <?php
    
    $taxonomy = 'product_cat';
    // Load all the terms for the product category
    $terms = get_terms( array(
        'taxonomy'   => $taxonomy,
        'hide_empty' => FALSE,
    ) );
    
    // Put all of the category id's into a simple array for searching
    $ids = array();
    foreach( $terms as $term ) {  
      $ids[] = $term->term_id; 
    }
    
    // Load the current term ID from the current query
    $this_term = get_query_var( 'term' );
    // Find the term in the list of ID's
    $this_position = array_search( $this_term, $ids );
    // Identify the previous term
    $prev_position = $this_position - 1;
    // Identify the next term
    $next_position = $this_position + 1;
    
    // IF the previous term exists, and is not "off" the list (the currently displayed term is not the first term), then display the link
    if( $prev_position >=0 ) {
        $prev_id = array_slice( $ids, $prev_position, 1 );
        $prev_term = get_term( $prev_id[0], $taxonomy );
        if ( $prev_term ) {
            echo '<a href="' . get_term_link( $prev_term->term_id, $taxonomy ) . '">&laquo; ' . $prev_term->name . '</a>'; 
        }
    }
    
     // IF the next term exists, and is not "off" the list (the currently displayed term is not the last term), then display the link
    if( $next_position > 0 && $next_position < count($ids) ) {
        $next_id = array_slice( $ids, $next_position, 1 );
        $next_term = get_term ($next_id[0], $taxonomy );
        if ( $next_term ) {
            echo '<a href="' . get_term_link( $next_term->term_id, $taxonomy ) . '">' . $next_term->name . ' &raquo;</a>'; 
        }
    }
    ?>