在父级的子类别中显示父级的其他子类别
Showing other subcategories of parent when in subcategory of parent
我想要一个关于类别及其子类别的简单概述。单击父类别时,仅显示该类别的子类别。但我也希望子类别在同一父级的不同子类别中显示。
我意识到 queried_object_id();单击子类别时会发生变化。但是如何让查询知道呢?
我的代码如下:
<ul>
<?php
$parentid = get_queried_object_id();
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
//echo $category_id;
echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .' - '. $category_id .'</a></li>';
//echo $parentid;
if($parentid == $category_id) {
//$cat = get_queried_object();
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
echo '<ul>';
foreach($sub_cats as $sub_category) {
//echo $parentid;
echo '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .' - '. $sub_category->term_id .'</a></li>';
//echo $sub_category->name ;
}
echo '</ul>';
}
}
}
}
?>
</ul>
为什么您不简单地将 woocommerce 小部件用于具有 Wordpress the_widget()
功能的产品类别,因为您将通过这种方式获得所有内容:
the_widget( 'WC_Widget_Product_Categories', array( 'hide_empty' => false, 'hierarchical' => true, 'show_children_only' => true ) );
它将显示链接产品类别的分层垂直列表。
With the_widget()
function you can with:
- the 2nd parameter argument make changes in the Query
- the 3rd parameter argument make changes in the html output
您可以通过
排除当前 category/term
'exclude' => $catID
在参数中。
我根据获取 $parentid2 = get_queried_object();
更改了代码
注:这段代码放在/woocommerce/archive-product.php
并且我添加了一个带有不同参数的 elseif 语句。
也许有点太多了,但它有效
<ul>
<?php
$parentid2 = get_queried_object();
$parentid = get_queried_object_id();
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></li>';
if($parentid == $category_id) {
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
echo '<ul>';
foreach($sub_cats as $sub_category) {
//echo $parentid;
echo '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a></li>';
//echo $sub_category->name ;
}
echo '</ul>';
}
}
elseif($parentid2->parent == $category_id) {
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $parentid2->parent,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
echo '<ul>';
foreach($sub_cats as $sub_category) {
//echo $parentid;
echo '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a></li>';
//echo $sub_category->name ;
}
echo '</ul>';
}
}
}
}
?>
</ul>
我想要一个关于类别及其子类别的简单概述。单击父类别时,仅显示该类别的子类别。但我也希望子类别在同一父级的不同子类别中显示。 我意识到 queried_object_id();单击子类别时会发生变化。但是如何让查询知道呢?
我的代码如下:
<ul>
<?php
$parentid = get_queried_object_id();
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
//echo $category_id;
echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .' - '. $category_id .'</a></li>';
//echo $parentid;
if($parentid == $category_id) {
//$cat = get_queried_object();
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
echo '<ul>';
foreach($sub_cats as $sub_category) {
//echo $parentid;
echo '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .' - '. $sub_category->term_id .'</a></li>';
//echo $sub_category->name ;
}
echo '</ul>';
}
}
}
}
?>
</ul>
为什么您不简单地将 woocommerce 小部件用于具有 Wordpress the_widget()
功能的产品类别,因为您将通过这种方式获得所有内容:
the_widget( 'WC_Widget_Product_Categories', array( 'hide_empty' => false, 'hierarchical' => true, 'show_children_only' => true ) );
它将显示链接产品类别的分层垂直列表。
With
the_widget()
function you can with:
- the 2nd parameter argument make changes in the Query
- the 3rd parameter argument make changes in the html output
您可以通过
排除当前 category/term'exclude' => $catID
在参数中。
我根据获取 $parentid2 = get_queried_object();
更改了代码注:这段代码放在/woocommerce/archive-product.php
并且我添加了一个带有不同参数的 elseif 语句。
也许有点太多了,但它有效
<ul>
<?php
$parentid2 = get_queried_object();
$parentid = get_queried_object_id();
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></li>';
if($parentid == $category_id) {
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
echo '<ul>';
foreach($sub_cats as $sub_category) {
//echo $parentid;
echo '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a></li>';
//echo $sub_category->name ;
}
echo '</ul>';
}
}
elseif($parentid2->parent == $category_id) {
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $parentid2->parent,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
echo '<ul>';
foreach($sub_cats as $sub_category) {
//echo $parentid;
echo '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a></li>';
//echo $sub_category->name ;
}
echo '</ul>';
}
}
}
}
?>
</ul>