Wordpress/Woocommerce-在Category archive-product.php上添加链接到相邻类别列表的上一个/下一个按钮

Wordpress/Woocommerce-在Category archive-product.php上添加链接到相邻类别列表的上一个/下一个按钮,wordpress,woocommerce,categories,Wordpress,Woocommerce,Categories,我已经在Woocommerce/archive-product-list.php中以我的子主题创建了我的产品类别模板,它是列表格式的。我已经在functions.php文件中指定了哪些woo commerce产品类别将使用哪些模板 我想将上一个和下一个类别链接添加到模板中,该模板将从下一个和上一个类别动态创建url,然后链接到相邻的woo commerce类别和/或子类别,该类别将显示该类别中的列表项 我发现很少有关于如何做到这一点的信息。大多数文章都是关于同一类别中的上一篇/下一篇文章,这不是

我已经在Woocommerce/archive-product-list.php中以我的子主题创建了我的产品类别模板,它是列表格式的。我已经在functions.php文件中指定了哪些woo commerce产品类别将使用哪些模板

我想将上一个和下一个类别链接添加到模板中,该模板将从下一个和上一个类别动态创建url,然后链接到相邻的woo commerce类别和/或子类别,该类别将显示该类别中的列表项

我发现很少有关于如何做到这一点的信息。大多数文章都是关于同一类别中的上一篇/下一篇文章,这不是我想要的

我发现的最接近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>'; 
$this_category=get_queryed_object();
$categories=get_categories();
foreach($position=>$cat的类别):
如果($this\u category->term\u id==$cat->term\u id):
$next_cat=$position+1;
$prev_cat=$position-1;
打破
endif;
endforeach;
$next\u cat=$next\u cat==计数($categories)?0:$next_cat;
$prev_cat=$prev_cat<0?计数($categories)-1:$prev_cat;
回声';回声';回音“

”;

我读过一些关于分类法和分页的讨论,但不知道这对我有什么帮助。任何帮助都将不胜感激,即使它只是为我指明了正确的方向

谢谢。

下面是适用于自定义分类法(WooCommerce的产品类别)的代码

我没有深入研究这个问题,以确保它按照您描述的顺序列出它们,逻辑可能会变得相当复杂,以解决围绕“这是一个孩子吗?这有孩子吗?下一步应该显示哪个分类法?”的问题,这也可能导致大量代码,并且可能不得不改变下面使用的策略。有关可用于排序的选项的一些想法,请参阅的文档,特别是orderby和order部分

另外请注意,下面的代码未经测试,因此请在下面的评论中提出澄清问题,我将尽力改进代码

<?php

$taxonomy = 'product_cat';
// Load all the terms for the product category
$terms = get_terms( array(
    'taxonomy'   => $taxonomy,
    'hide_empty' => FALSE,
) );

// Put all of the category id's into a simple array for searching
$ids = array();
foreach( $terms as $term ) {  
  $ids[] = $term->term_id; 
}

// Load the current term ID from the current query
$this_term = get_query_var( 'term' );
// Find the term in the list of ID's
$this_position = array_search( $this_term, $ids );
// Identify the previous term
$prev_position = $this_position - 1;
// Identify the next term
$next_position = $this_position + 1;

// IF the previous term exists, and is not "off" the list (the currently displayed term is not the first term), then display the link
if( $prev_position >=0 ) {
    $prev_id = array_slice( $ids, $prev_position, 1 );
    $prev_term = get_term( $prev_id[0], $taxonomy );
    if ( $prev_term ) {
        echo '<a href="' . get_term_link( $prev_term->term_id, $taxonomy ) . '">&laquo; ' . $prev_term->name . '</a>'; 
    }
}

 // IF the next term exists, and is not "off" the list (the currently displayed term is not the last term), then display the link
if( $next_position > 0 && $next_position < count($ids) ) {
    $next_id = array_slice( $ids, $next_position, 1 );
    $next_term = get_term ($next_id[0], $taxonomy );
    if ( $next_term ) {
        echo '<a href="' . get_term_link( $next_term->term_id, $taxonomy ) . '">' . $next_term->name . ' &raquo;</a>'; 
    }
}
?>


为了回答这个问题,您必须定义上一个类别(和下一个类别)的含义。有什么分类顺序吗?你查过这个链接了吗这个概念是相同的,但是有分类功能(而不是类别功能*),因为产品类别是一个自定义的分类法。我正在回顾你发给我的内容。非常感谢。我希望的顺序是从最高级别的家长到孩子,然后再到下一个级别的最高级别,依此类推。再说一遍,“下一个”级别是什么?排序顺序是什么?我已经准备好了代码,现在我正试图用分类法函数替换类别函数,但我对正确的切换方式有点迷茫。感谢您的帮助。我会继续研究分类功能。谢谢。下一个类别将是子类别和或wordpress后端中的上一个/下一个类别。我有点困惑,很抱歉,如果我没有回答正确。谢谢你的时间和关注。在接下来的几天里,我将继续解决这个问题。你就是那个人。非常感谢你为我指明了正确的方向。我会发回更新。