Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Sorry, I’m not sure I follow. I think a solution is to add the Categories widget to your sidebar area, selecting the display as dropdown check box. This of course results in a list of all of your categories. To customize it, use the ‘widget_categories_dropdown_args’ filter. What you return from your callback here is used as arguments to a call to wp_dropdown_categories(). You can customize the output by altering the arguments to the function.

    The wp_dropdown_categories() function has its own filters with which you can further customize the output if needed.

    Thread Starter Sewlicious

    (@sewlicious)

    Unfortunately, I am not familiar with how to use that. What I’m trying to do is prevent some categories from showing up in the dropdown menu

    • This reply was modified 7 years, 5 months ago by Sewlicious.
    Moderator bcworkz

    (@bcworkz)

    That’s possible if wp_dropdown_categories() is involved, you can pass an array of excluded IDs as the “exclude” argument. Please confirm exactly how your menu is being generated. The default categories widget? I want to be sure I’m giving you the correct filter hook. I can show you an example of how this is all done if I have the correct information.

    To better understand the forthcoming example, it’s good for some to see where it’s applied. (if using the categories widget) A general description of filter hooks is useful if these are new to you.

    These may not make much sense without the example, or depending on your coding aptitude, it’s enough to work out the solution.

    Thread Starter Sewlicious

    (@sewlicious)

    Using the category widget is the only way I know how to show my categories. I am not a coding expert by any means lol.

    Moderator bcworkz

    (@bcworkz)

    That’s fine, just making sure. (the list can be added anywhere on a template by calling the wp_dropdown_categories() function, but requires a different way to exclude categories) You’ll need to compile a list of term IDs you want to exclude and use that in place of the ID numbers in my example. You can get them from the category list table screen. The edit link seen on hover over each term includes a tag_ID URL parameter. The number following this is the ID for that term. Compile into a comma separated list on a single line.

    It appears you are using a child theme, so its functions.php file is a good place for this code, just add it to the end of the file. This code will negate any other exclusion arguments added by other code. If that happens, there’s a fix, but the code gets more complicated, so I’d just as soon avoid it if it’s not needed. Also, if any code is adding an “include” argument, this will cause our added exclusions to be ignored.

    Without further ado, here’s the code to add and in which to edit IDs:

    add_filter('widget_categories_dropdown_args', 'shd_exclude_cats');
    function shd_exclude_cats( $cat_args ) {
       // term IDs to exclude go in the following array, comma separated
       $cat_args['exclude'] = array( 123, 124, 125,); 
       return $cat_args;
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Customized Category Dropdown menu’ is closed to new replies.