The sub_cat_1 function outputs an array under categories of main category 1

function sub_cat_1(){ $term_id = 1; $taxonomy_name = 'category'; $term_children = get_term_children( $term_id, $taxonomy_name ); foreach ( $term_children as $child ) { $term = get_term_by( 'id', $child, $taxonomy_name ); echo '' . $term->term_id . ','; } } 

It is necessary to display this array under categories in the function custom_posts_per_page in the is_category condition

 function custom_posts_per_page($query){ if( $query->is_category(sub_cat_1()) && $query->is_main_query() && !is_admin() ){ //обработка } } 

The first function correctly displays all sub categories of the specified category 1 in the required form for is_category () . The second function does not get these under categories in is_category () , but brings them to the very top of the site before. Also did before the condition like this:

 function custom_posts_per_page($query){ $ids = sub_cat_1(); if( $query->is_category($ids) && $query->is_main_query() && !is_admin() ){} } 

And also brings the array to the very top of the site, as I understand the problem in the first function, in foreach Tell me how to correctly output to the condition

    1 answer 1

     function sub_cat_1(){ $term_id = 1; $taxonomy_name = 'category'; $term_children = get_term_children( $term_id, $taxonomy_name ); $cats = array(); foreach ( $term_children as $child ) { $term = get_term_by( 'id', $child, $taxonomy_name ); $cats[] = $term->term_id; } return implode(',', $cats); }