categories - How to display get_the_category of post showing only CHILD of category "X"

admin2025-06-06  8

I'm trying to display the get_category_category limiting the results to only the child categories of parent "666".

So a post may have a number of categories e.g. "1234", "666", "666/1341", "5", "5/17" but I only want to show the child of "666".

$args = array( 'numberposts' => '5','post_status' => 'publish','category' => '1234','category__not_in' => '680', 'orderby' => 'post_date', 'order' => 'DESC' );
$recent_posts = wp_get_recent_posts($args);
{

///I want to return the category for each post in this loop that is within the "666" category.
///E.g get_the_category($recent['ID']) where the category is a child of 666

echo "POST ID" . $recent['ID'] . " CHILD CATEGORY ". $childcategory; 
}

The above loop will return the last 5 posts in category "1234" with each child category of "666".

e.g.

  • POST ID 10 CHILD CATEGORY 1341
  • POST ID 11 CHILD CATEGORY 1341
  • POST ID 14 CHILD CATEGORY 99
  • POST ID 19 CHILD CATEGORY 1341
  • POST ID 23 CHILD CATEGORY 99

I'm trying to display the get_category_category limiting the results to only the child categories of parent "666".

So a post may have a number of categories e.g. "1234", "666", "666/1341", "5", "5/17" but I only want to show the child of "666".

$args = array( 'numberposts' => '5','post_status' => 'publish','category' => '1234','category__not_in' => '680', 'orderby' => 'post_date', 'order' => 'DESC' );
$recent_posts = wp_get_recent_posts($args);
{

///I want to return the category for each post in this loop that is within the "666" category.
///E.g get_the_category($recent['ID']) where the category is a child of 666

echo "POST ID" . $recent['ID'] . " CHILD CATEGORY ". $childcategory; 
}

The above loop will return the last 5 posts in category "1234" with each child category of "666".

e.g.

  • POST ID 10 CHILD CATEGORY 1341
  • POST ID 11 CHILD CATEGORY 1341
  • POST ID 14 CHILD CATEGORY 99
  • POST ID 19 CHILD CATEGORY 1341
  • POST ID 23 CHILD CATEGORY 99
Share Improve this question asked Mar 4, 2018 at 2:11 Graham BrownGraham Brown 313 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

You can do this by getting the child categories of that category first, then passing them as an array to your query.

$categories=get_categories(
    array( 'parent' => $cat->cat_ID )
);

$cat_list = [];
foreach ($categories as $c) {
    $cat_list[] = $c->term_id;
}

ref: Get the children of the parent category

This won't/might not work, but it's usually worth trying passing an array where you could pass an integer in WordPress:

$args = array( 'numberposts' => '5','post_status' => 'publish','category' => $cat_list,'category__not_in' => '680', 'orderby' => 'post_date', 'order' => 'DESC' );

wp_get_recent_posts uses get_posts & get_post accepts an ID of a category, at the time of writing it doesn't mention an array... So you might need to use WP_Query:

$query = new WP_Query( array( 'category__and' => $cat_list ) );

see: https://codex.wordpress/Class_Reference/WP_Query

You can use array_map so you can return only the categories that you want. For example:

array_map( function( $cat ) {
        if ( $cat->parent != 0 ) {
            return $cat;
        }
     },
     get_the_category()
);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749220533a317372.html

最新回复(0)