获取父类别的孩子
4 个回答
- 投票数
-
- 2012-11-29
您不能只将字符串"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>'; }
You can't just pass the string "parent" to
get_categories
. You have to pass the ID of the parent.$categories=get_categories( array( 'parent' => $cat->cat_ID ) );
Notice that there are two similar but not equal "get child" parameters that you can use.
child_of (integer) Display all categories that are descendants (i.e. children & grandchildren) of the category identified by its ID. There is no default for this parameter. If the parameter is used, the hide_empty parameter is set to false.
parent (integer) Display only categories that are direct descendants (i.e. children only) of the category identified by its ID. This does NOT work like the 'child_of' parameter. There is no default for this parameter. [In 2.8.4]
Now you need to loop over the
$categories
. You can't just echo an array.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>'; }
-
不幸的是,这只是给我Array的输出.没有值被引入.Unfortunately, that is just giving me an output of Array. No values are being pulled in.
- 0
- 2012-11-29
- Chris Da Sie
-
当您尝试回显数组时,将发生"数组".您需要遍历数组并回显各个元素.'Array' is what happens when you try to echo an array. You need to loop over the array and echo the individual elements.
- 0
- 2012-11-29
- s_ha_dum
-
您可能要添加'hide_empty'=>false.也显示空类别.You might want to add 'hide_empty' => false. To also show empty categories.
- 2
- 2018-06-18
- Floris
-
- 2018-04-25
在您的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>'; } } ?>
Use the code below in your archive.php file. This code will help you:
<?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>'; } } ?>
-
请** [编辑]您的答案**,并添加说明:**为什么**可以解决问题?Please **[edit] your answer**, and add an explanation: **why** could that solve the problem?
- 0
- 2018-04-25
- fuxia
-
- 2019-12-22
如果数组中没有值,则可以尝试以下方法:
$last_categories = get_categories( array( 'taxonomy' => 'product_cat', 'parent' => $sub_category->cat_ID ) );
If there are no values in the array you can try the following approach:
$last_categories = get_categories( array( 'taxonomy' => 'product_cat', 'parent' => $sub_category->cat_ID ) );
-
- 2020-03-02
要获取子类别,可以使用以下代码.
$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来显示类别,其中没有任何帖子. 然后使用$ categories数组循环并进行标记.
To get child categories you can use following code.
$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 ) );
Notice :- I have used 'hide_empty' => false to show categories with no any posts under it. Then use the $categories array to loop and make your markup.
我试图在此循环中显示所有子类别,但是我在代码中苦苦挣扎.到目前为止,这就是我所拥有的.
任何帮助都会很棒