1 回答
TA贡献1780条经验 获得超5个赞
更新:
您只需定义第一个和最后一个类别 ID。然后它将作为无限循环工作(假设您的产品类别术语是连续的):
<?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; ?>
要获得最后一个类别,您可以尝试以下操作
- 1 回答
- 0 关注
- 99 浏览
添加回答
举报