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 關注
- 145 瀏覽
添加回答
舉報