<?php $args=array('orderby' => 'name', 'order' => 'ASC');
$categories=get_categories($args);
foreach ($categories as $cat) { ?>
<dt><a href="#" class="customer-acquisitiontop" id="<?php echo $cat->slug; ?>" data-filter=".<?php echo $cat->slug; ?>"><h2><?= $cat->cat_name; ?></h2></a></dt>
<dd><div class="services">
<?= $categories=get_categories('parent'); ?> /*This should be the children of the parent category */
</div>
</dd>
<?php } ?>
任何帮助都将是很大的
#1 楼
您不能只将字符串“ parent”传递给get_categories
。您必须传递父代的ID。$categories=get_categories(
array( 'parent' => $cat->cat_ID )
);
请注意,可以使用两个相似但不相等的“获取子代”参数。
child_of
(整数)显示其ID所标识的类别的所有后代(即子孙)。该参数没有默认值。如果使用该参数,则
hide_empty参数设置为false。
父
(整数)仅显示由其ID标识的类别的直接后代(即仅儿童)的类别。
不能像'child_of'参数那样工作。此
参数没有默认值。 [在2.8.4中]
现在您需要遍历
$categories
。您不能只回显数组。foreach ($categories as $c) {
var_dump($c);
// what you really want instead of var_dump is something to
// to create markup-- list items maybe, For example...
echo '<li>'.$c->cat_name.'</li>';
}
#2 楼
在您的archive.php文件中使用以下代码。此代码将为您提供帮助:
<?php
$term = get_queried_object();
$children = get_terms( $term->taxonomy, array(
'parent' => $term->term_id,
'hide_empty' => false
) );
if ( $children ) {
foreach( $children as $subcat )
{
echo '<li><a href="' . esc_url(get_term_link($subcat, $subcat->taxonomy)) . '">' . $subcat->name . '</a></li>';
}
}
?>
评论
请编辑您的答案,并添加解释:为什么这可以解决问题?
– fuxia♦
18年4月25日在7:57
#3 楼
如果数组中没有值,则可以尝试以下方法:$last_categories = get_categories(
array(
'taxonomy' => 'product_cat',
'parent' => $sub_category->cat_ID
)
);
#4 楼
要获取子类别,您可以使用以下代码。$category = get_queried_object(); // this is for getting the parent category on archive or any place the category object is called.
$categories=get_categories(
array( 'parent' => $category->term_id,
'hide_empty' => false )
);
注意:-我已经使用'hide_empty'=> false来显示类别,其中没有任何帖子。 br />然后使用$ categories数组循环并进行标记。
评论
不幸的是,这只是给我Array的输出。没有值被引入。
–克里斯·达西(Chris Da Sie)
2012年11月29日16:37
当您尝试回显数组时,将发生“数组”。您需要遍历数组并回显各个元素。
– s_ha_dum
2012年11月29日在16:39
您可能要添加'hide_empty'=> false。也显示空类别。
–花
18年6月18日在14:09