$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - How to display get_the_category of post showing only CHILD of category "X"|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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()
);
Post a comment

comment list (0)

  1. No comments so far