简体   繁体   中英

WooCommerce product category pages with previous and next term links

I want to customize WooCommerce category page (archive-product.php) to show current product category, but also I want to create Previous & Next links to previous and next category term links somewhere in the page.

I've tried a lot of solutions with no luck. I've manage to implement a code that take actual category ID, I've added +1 and -1 and I was able to create the links but the big issue is when 'next link' is on the last category that is not working, is not infinite loop for the links. IE if I'm on cat page 6 that is last category I have, next one should be 1 and prev one should be 5 and if I'm on first category, prev one should be 6 and next one should be 2.

Please find below the piece of code I`m using and is working but not as I need:

<?php
$taxonomy = 'product_cat';
$cate = get_queried_object();
$cateID = $cate->term_id;

$next_cateID = $cateID + 1;
$prev_cateID = $cateID - 1; 

$next_term_link = get_term_link( $next_cateID, $taxonomy );
$next_term = get_term( $next_cateID, $taxonomy );
$next_slug = $next_term->name;

$prev_term_link = get_term_link( $prev_cateID, $taxonomy );
$prev_term = get_term( $prev_cateID, $taxonomy );
$prev_slug = $prev_term->name;
?>

<p><a href="<?php echo $prev_term_link ?>"><?php echo $prev_slug; ?></a></p>
<p><a href="<?php echo $next_term_link ?>"><?php echo $next_slug; ?></a></p>

Updated:

You just need to define your first and your last categories Ids. Then it will work as an infinite loop (assuming that your product category terms are sequential) :

<?php
if ( is_product_category() ) :
$taxonomy    = 'product_cat'; // WooCommerce product category taxonomy
$term_id     = get_queried_object_id(); // Current product category term Id

// Get first product category term Id
$query_args  = array('taxonomy' => 'product_cat', 'hide_empty' => false, 'orderby' => 'term_id', 'number' => '1', 'fields' => 'ids');
$term_id_1st = get_terms($query_args);
$term_id_1st = reset($term_id_1st);

// Get last product category term Id
$query_args['order'] = 'DESC';
$term_id_end  = get_terms($query_args);
$term_id_end  = reset($term_id_end);

$next_term_id = $term_id_end == $term_id ? $term_id_1st : $term_id + 1;
$prev_term_id = $term_id_1st == $term_id ? $term_id_end : $term_id - 1; 

$next_term_link = get_term_link( $next_term_id, $taxonomy );
$next_term      = get_term( $next_term_id, $taxonomy );

$prev_term_link = get_term_link( $prev_term_id, $taxonomy );
$prev_term      = get_term( $prev_term_id, $taxonomy );

?>
<p><a href="<?php echo $prev_term_link ?>"><?php echo $prev_term->name; ?></a></p>
<p><a href="<?php echo $next_term_link ?>"><?php echo $next_term->name; ?></a></p>
<?php endif; ?>

To get the last category you can try the following

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM