0

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));
?>
1
  • That error could happen when the $cat_now is empty (no items in the array).
    – Sally CJ
    Commented Jul 22, 2019 at 10:33

1 Answer 1

1

get_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 category_parent, but just parent., which have 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);
}
3
  • I'd also suggest using the parent property, but the category_parent property does (still) exist and is added by _make_cat_compat().
    – Sally CJ
    Commented Jul 22, 2019 at 15:44
  • 1
    @SallyCJ Thank you, I haven't noticed that before. I updated my answer. Commented Jul 22, 2019 at 18:14
  • Thank you for your reply. Try this code.
    – user172077
    Commented Jul 23, 2019 at 2:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.