我在这个论坛上搜索了可能的解决方案,但没有找到任何有效的方法。也许我做得不对。
这是我迄今为止尝试过的代码
$parent_categories = \'\' ;
$sub_categories = \'\';
$subcat = \'\';
$args = array(
\'number\' => $number,
\'orderby\' => $orderby,
\'order\' => $order,
\'hide_empty\' => $hide_empty,
\'include\' => $ids,
\'hierarchical\'=> true,
\'parent\' => 0
);
$product_categories = get_terms( \'product_cat\', $args );
foreach ($product_categories as $cat) {
//var_dump($cat); echo \'<br>\';
如果($cat->slug==\'文章\'){
$args2 = array(
\'number\' => $number,
\'orderby\' => $orderby,
\'order\' => $order,
\'hide_empty\' => $hide_empty,
\'include\' => $ids,
\'parent\' => $cat->term_id,
\'hierarchical\'=> true,
);
$sub_categories = get_terms( \'product_cat\', $args2 );
foreach ($sub_categories as $subcat) {
$sub_categories = $sub_categories.$subcat->name.\',\';
}
$sub_categories = rtrim($sub_categories);
}
else
$sub_categories= \'nomatch\';
}
当我这样做的时候
echo $sub_categories
.
我得到输出no match
;
Debug results
我尝试删除if-else条件,以查看哪些条件有效,哪些条件无效。
$parent_categories = \'\' ;
$sub_categories = \'\';
$subcat = \'\';
$args = array(
\'number\' => $number,
\'orderby\' => $orderby,
\'order\' => $order,
\'hide_empty\' => $hide_empty,
\'include\' => $ids,
\'hierarchical\'=> true,
\'parent\' => 0
);
$product_categories = get_terms( \'product_cat\', $args );
foreach ($product_categories as $cat) {
//var_dump($cat); echo \'<br>\';
$args2 = array(
\'number\' => $number,
\'orderby\' => $orderby,
\'order\' => $order,
\'hide_empty\' => $hide_empty,
\'include\' => $ids,
\'parent\' => $cat->term_id,
\'hierarchical\'=> true,
);
$sub_categories = get_terms( \'product_cat\', $args2 );
foreach ($sub_categories as $subcat) {
$sub_categories = $sub_categories.$subcat->name.\',\';
}
$sub_categories = rtrim($sub_categories);
代码确实检索父类别,并且
ALL 的子类别
ALL 顶级类别。尽管我想要特定父类别的子类别。我试过了
parent=>$cat->term_id
和
$cat->term_taxonomy_id
. 结果仍然是一样的。我尝试对家长的term\\u id进行硬编码,但没有用。
好的,所以我的第一个任务是获取特定类别的子类别!
我的第二个问题是,为什么if条件(用橙色标记)不能很好地工作?
请让我知道我要在这里做什么修改!
SO网友:websupporter
作为第一个条件:如果检索子类别,是否尝试过?
$sub_categories = get_terms( \'product_cat\', $args2 );
echo \'<pre>\';print_r( $sub_categories );
如果您有子类别,您的
foreach
:
foreach ($sub_categories as $subcat) {
$sub_categories = $sub_categories.$subcat->name.\',\';
}
您正在覆盖
$sub_categories
第一次进入循环。所以,如果你有子类别,你仍然会有一个问题。尝试以下操作
$text = \'\';
$i = 0;
foreach ($sub_categories as $subcat){
if( $i > 0 )
$text .= \', \';
$text .= $subcat->name;
$i = 1;
}
echo $text;
因此,考虑到您的第一个代码,这可能会有帮助:
<?php
$parent_categories = \'\' ;
$sub_categories = \'\';
$subcat = \'\';
$args = array(
\'number\' => $number,
\'orderby\' => $orderby,
\'order\' => $order,
\'hide_empty\' => $hide_empty,
\'include\' => $ids,
\'hierarchical\'=> true,
\'parent\' => 0
);
$product_categories = get_terms( \'product_cat\', $args );
foreach( $product_categories as $cat ) {
if($cat->slug == \'essays\') {
$args2 = array(
\'number\' => $number,
\'orderby\' => $orderby,
\'order\' => $order,
\'hide_empty\' => $hide_empty,
\'include\' => $ids,
\'parent\' => $cat->term_id,
\'hierarchical\'=> true,
);
$sub_categories = get_terms( \'product_cat\', $args2 );
$text = \'\';
$i = 0;
if( is_array( $sub_categories ) && count( $sub_categories ) > 0 ){
foreach ($sub_categories as $subcat){
if( $i > 0 )
$text .= \', \';
$text .= $subcat->name;
$i = 1;
}
$sub_categories; = $text;
} else {
$sub_categories= \'nomatch\';
}
}
}
?>