主页>技术社区>编程问答

WooCommerce产品类别页面,带有上一个和下一个链接

eIT.com.cn 2023/3/17 11:23:37 阅读 4 次

打印


如何解决WooCommerce产品类别页面,带有上一个和下一个链接?

我想WooCommerce类别(archive-product.)以当前产品类别,但我也想在中的某处创建指向上和下类别术语的“上”和“下”。

我没有运气就尝试了很多案。我已经设法实现了带有实际类别ID的,了+1和-1并且能够创建,但是最大的问题是,当“下”位于最后无法正常工作的类别时,不是的无限循环。 IE,如果我在第6页上属于我的最后类别,则下应该是1,上应该是5;如果我在第类别中,上应该是6,下应该是2。 >

请在下面使用我正在使用的,并且正在运行,但不是我需要的:

<?
$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="<? echo $prev_term_link ?>"><? echo $prev_slug; ?></a></p>
<p><a href="<? echo $next_term_link ?>"><? echo $next_slug; ?></a></p>

解决方法

已更新:

您只需要定义您的第一个和最后一个类别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; ?>

要获取最后一个类别,您可以尝试以下






相关内容


热门栏目


特别声明


最新资讯
热讯排行



合作媒体友情链接
生活常识小贴士 软件开发教程 智慧城市生活网 息县通生活服务[移动版] 息县商圈[移动版] 美食菜谱
健康养生 法律知识 科技频道 电影影讯 留学考研学习 星座生肖|解梦说梦




关于我们 | 联系我们 | 合作媒体 | 使用条款 | 隐私权声明 | 版权声明

      Copyright © 2023 eIT.com.cn. All Rights Reserved. 豫ICP备2022012332号