0

Is there a way to exclude all categories except one? I want to show a category and it's sub-categories as a dropdown menu, but the admin might add more subcategories to it so I don't want to limit what they can put in there by just including unique ID's.

So I want to exclude all categories except 1 and it's subcategories. Is this possible with wp_list_categories()?

1

Try the exclude parameter in the wp_list_categories. That should remove the selected categories and their descendants.

EDIT: Must be getting late; this almost the opposite of what you wanted. To only include one category term in a drop-down menu (and all of its descendants), you can use child_of.

  • Right but if the admin adds more categories and sub categories I would then have to go in and exclude their specific ID everytime. I'm trying to find something flexible. – Howdy_McGee Jan 26 '12 at 16:18
  • You said you want to exclude all categories except 1... If you want to include only specific categories (i.e. pre-defined ones that a client hasn't added, you'll want include instead.) – Stephen Harris Jan 26 '12 at 16:20
  • If he uses 'include', does he have to include children IDs as well? – Shane Jan 26 '12 at 16:24
  • 1
    Yes, or he could use your solution; I don't think there's an automatic way of doing it. But if he just want's to show a category term and all its subcategories then he can just use the child_of. – Stephen Harris Jan 26 '12 at 16:32
0

Theres possibly a better way of doing this, but this should work :

 $the_terms = get_terms( 'category', 'child_of=<replace with parent id>' );

 $included_terms = array('<replace with parent id>' );

 if( $the_terms )
 {
      foreach( $the_terms as $the_term )
           $included_terms[] = $the_term->term_id;
 }

 sort( $included_terms );

 wp_list_categories(
      'include' => $included_terms
 );

This way will make sure you don't have to update the list of IDs

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.