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