0

I have next code and i cant get it to list only the subcategories of a category and not sub-subcategories of the category.

I think the problem is the next line

    'hierarchical' => 0,

Do you know what can be the problem or what should i add in the next code to fix for what i want.

I've set it to 1 to 0, and nothing changes. This is a custom theme for a directory.

<?php // Get variables

global $def_cat_style;

$get_id_parent_cat = get_term_by( 'slug', $company_category, 'company_category' );

$args = array(

    'child_of' => $get_id_parent_cat->term_id ,

    'parent' => get_query_var(''),

    'orderby' => 'name',

    'order' => 'ASC',

    'hide_empty' => 0,

    'hierarchical' => 0,

    'taxonomy' => 'company_category',

    'pad_counts' => true );

$categories = get_categories($args);

?>

Thank you.

8
  • It looks like you need to provide the other PHP code, and you need to tell the name of the project that contains that code. Did you try to set the 'hierarchical' to 3? – 5ervant Aug 5 '15 at 14:38
  • My full code is this: pastebin.com/fh5VUVFt , the problem is that i need to get 'depth' => 1, (only top level subcategories of the category you're on). And from what i reserch its not supported by get_categories it's supported by wp_list_categories, but it outputs it as a simple list, and i need that custom build i have because has css style and some options also build in theme. Maybe is a work around to do this with get_categories. Or if somebody know how can i output exactly like the one with get_categories using instead wp_list_categories. Regards. – Alex Grecu Aug 6 '15 at 7:49
  • I tryed something myself, but i think it has some problems with the echo, if, else.. pastebin.com/XXN0bv0L could help me, would it work? – Alex Grecu Aug 6 '15 at 8:27
  • I guess you need 'depth' => 2. – 5ervant Aug 6 '15 at 9:06
  • 1
    I got it working by using as parent the same value as child_of, thank you for your time. I used this: 'parent' => $get_id_parent_cat->term_id, – Alex Grecu Aug 6 '15 at 9:51
1

I would do something like this:

$categories = get_categories($args);
foreach ($categories as $category){
    if ($category->parent != $get_id_parent_cat->term_id){
        //Do stuff with the category
    }
}
1
  • My full code is this: pastebin.com/fh5VUVFt if i add what you said it's the same, i think i have to do something else. Thanks for your help. – Alex Grecu Aug 5 '15 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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