admin管理员组文章数量:1419202
You are currently adding two parent categories: Events and Magazine.
The magazine has child categories associated with interviews and reviews.
Below is a hierarchical representation of the current category. There are currently no posts in the Review category.
Events
Magazine
Magazine > Interviews
Magazine > Review
This is the main point. I want to know how to get the parent category ID from the current category ID and list the names of the child categories.
Below are the problems that are occurring.
If you access and confirm a child category that does not have a post, an unrelated event category will be output.
It also prints an error.
Notice: Undefined offset: 0 in /var/www/html/custom/wp-content/themes/cocrework/category.php on line 6
Notice: Trying to get property 'category_parent' of non-object in /var/www/html/custom/wp-content/themes/cocrework/category.php on line 7
The following code is used.
<?php
$cat_now = get_the_category();
$cat_now = $cat_now[0];
$parent_id = $cat_now->category_parent;
$args = array(
'child_of' => $parent_id,
);
var_dump(wp_list_categories($args));
?>
You are currently adding two parent categories: Events and Magazine.
The magazine has child categories associated with interviews and reviews.
Below is a hierarchical representation of the current category. There are currently no posts in the Review category.
Events
Magazine
Magazine > Interviews
Magazine > Review
This is the main point. I want to know how to get the parent category ID from the current category ID and list the names of the child categories.
Below are the problems that are occurring.
If you access and confirm a child category that does not have a post, an unrelated event category will be output.
It also prints an error.
Notice: Undefined offset: 0 in /var/www/html/custom/wp-content/themes/cocrework/category.php on line 6
Notice: Trying to get property 'category_parent' of non-object in /var/www/html/custom/wp-content/themes/cocrework/category.php on line 7
The following code is used.
<?php
$cat_now = get_the_category();
$cat_now = $cat_now[0];
$parent_id = $cat_now->category_parent;
$args = array(
'child_of' => $parent_id,
);
var_dump(wp_list_categories($args));
?>
Share
Improve this question
asked Jul 22, 2019 at 3:26
user172077user172077
1
1
|
1 Answer
Reset to default 1get_the_category()
should either be used within a (post) loop or with a $post_id
parameter. In your code $cat_now
is probably at the moment an empty array, so there's nothing at the 0 position.
Also, get_the_category()
returns an array of WP_Term
objects, which don't have a property called , which have category_parent
, but just parent
.parent
property that you should use.
If you want to list child categories for a certain category in category.php
, then the following code should work.
/**
* For category.php
*/
$current_category = get_queried_object(); // should be WP_Term object in this context
$category_list = '';
// $current_category->parent is 0 (i.e. empty), if top level category
if ( ! empty( $current_category->parent ) ) {
$category_list = wp_list_categories(array(
'child_of' => $current_category->parent,
));
// var_dump($category_list);
}
And just for fun, example for getting children of the first category of a post.
/**
* Used e.g. in single.php
* get_the_id() is not required, if get_the_category() is used in the Loop
*/
$current_categories = get_the_category( get_the_id() ); // Array of WP_Term objects, one for each category assigned to the post.
$category_list = '';
if ( ! empty( $current_categories[0]->parent ) && $current_categories[0]->parent ) {
$category_list = wp_list_categories(array(
'child_of' => $current_categories[0]->parent,
));
// var_dump($category_list);
}
本文标签: I want to know how to output child categories related to parent categories
版权声明:本文标题:I want to know how to output child categories related to parent categories 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745304768a2652578.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$cat_now
is empty (no items in the array). – Sally CJ Commented Jul 22, 2019 at 10:33