To add a custom taxonomy to a custom menu in WordPress, you can use the wp_nav_menu_objects filter to modify the menu items before they are displayed. This filter allows you to add custom menu items programmatically based on your custom taxonomy.

Here's an example of how to achieve this:

  1. Register your custom taxonomy (if you haven't already):

You should register your custom taxonomy in your theme's functions.php file or in a custom plugin. For example:

function custom_taxonomy() { $args = array( 'hierarchical' => true, 'labels' => array( 'name' => 'Custom Taxonomy', 'singular_name' => 'Custom Taxonomy', ), // Add other arguments as needed. ); register_taxonomy('custom_taxonomy', 'your_custom_post_type', $args); } add_action('init', 'custom_taxonomy');

Replace 'your_custom_post_type' with the name of your custom post type to which the custom taxonomy is attached.

  1. Use the wp_nav_menu_objects filter to add custom taxonomy terms to the menu:
function add_custom_taxonomy_to_menu($items, $args) { if ($args->theme_location === 'your_custom_menu_location') { $taxonomy = 'custom_taxonomy'; // Replace with your custom taxonomy slug. $terms = get_terms($taxonomy, array('hide_empty' => false)); if (!empty($terms)) { foreach ($terms as $term) { $menu_item = new stdClass(); $menu_item->ID = -1; // A unique negative ID for the custom menu item. $menu_item->db_id = 0; $menu_item->title = $term->name; $menu_item->url = get_term_link($term, $taxonomy); $menu_item->menu_item_parent = 0; $menu_item->type = 'taxonomy'; $menu_item->object = 'custom_taxonomy'; // Custom taxonomy slug. $menu_item->object_id = $term->term_id; $menu_item->classes = array(); $menu_item->target = ''; $menu_item->xfn = ''; // Add the custom menu item to the menu. $items[] = $menu_item; } } } return $items; } add_filter('wp_nav_menu_objects', 'add_custom_taxonomy_to_menu', 10, 2);

In this example, replace 'your_custom_menu_location' with the name of your custom menu location where you want to add the custom taxonomy items. Also, replace 'custom_taxonomy' with your custom taxonomy slug.

Now, when you go to the "Appearance" -> "Menus" section in your WordPress admin, you should see the custom taxonomy terms available to add to your custom menu.

Please note that you need to have some custom taxonomy terms already created to see them in the custom menu. Additionally, this code assumes you have already set up a custom post type and attached the custom taxonomy to it. Adjust the code according to your specific setup.

Have questions or queries?
Get in Touch