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);
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745304769a4621643.html
$cat_now
is empty (no items in the array). – Sally CJ Commented Jul 22, 2019 at 10:33