I have lot of categories and sub categories. I want to add all these in WordPress menu. But i want to add subcategories aligned properly under their parent categories without drag and drop each and every time.
Right now when i select all menus from categories these does not aligned under their parent category.Is there any automated way to do this so it will save my lot of my time?
Your help will be much appreciated.
-
Nope, but there's a great plugin that can help you. It let's you choose the option to automatically fetch sub-categories.– gdanielCommented Aug 18, 2017 at 18:34
2 Answers
You can use this function:
the_widget( 'WP_Widget_Categories', 'dropdown=1&hierarchical=1' );
This function give you a select box and you will get all the parent categories and sub-categories. After applying this code inspect category select box, then you can manipulate your HTML code. And show your category as you wish.
If you don't want any select box, then you just remove "downdown=1&"
. This will give you a whole list of all categories and sub-categories. After applying this code inspect category select box, then you can manipulate your HTML code. And show your category as you wish.
Hope this will help!
-
Thank you so much for your answer. But i ll need this for wordpress menu in admin panel. So that i will not need to drag sub categories to align properly. In WordPress admin panel i can see different classes for menus alignment "menu-item-depth-0". But there is no such class to target child categories so that i can change this with some jQuery. Commented Aug 19, 2017 at 9:24
Steps to Automatically Add Categories and Subcategories to a WordPress Menu:
Create a Custom Function to Add Categories to Menu: You can add a custom PHP function in your theme’s functions.php file that automatically generates a hierarchical menu structure based on your categories and subcategories.
Modify the Menu Automatically: Hook this function to automatically add categories and subcategories to a specific menu whenever the categories are updated.
function add_categories_to_menu( $menu_id ) {
$categories = get_categories( array(
'hide_empty' => false,
) );
$menu_items = array();
// First, create an array of parent categories
foreach ( $categories as $category ) {
if ( $category->parent == 0 ) {
$menu_items[ $category->term_id ] = array(
'category' => $category,
'subcategories' => array(),
);
}
}
// Then, loop through categories again to assign subcategories
foreach ( $categories as $category ) {
if ( $category->parent != 0 && isset( $menu_items[ $category->parent ] ) ) {
$menu_items[ $category->parent ]['subcategories'][] = $category;
}
}
// Now, add the categories and subcategories to the menu
foreach ( $menu_items as $parent_category ) {
// Add parent category
wp_update_nav_menu_item( $menu_id, 0, array(
'menu-item-title' => $parent_category['category']->name,
'menu-item-url' => get_category_link( $parent_category['category']->term_id ),
'menu-item-status' => 'publish',
));
// Add subcategories under the parent category
foreach ( $parent_category['subcategories'] as $subcategory ) {
wp_update_nav_menu_item( $menu_id, 0, array(
'menu-item-title' => $subcategory->name,
'menu-item-url' => get_category_link( $subcategory->term_id ),
'menu-item-status' => 'publish',
'menu-item-parent-id' => $parent_category['category']->term_id,
));
}
}
}
// Use the function by calling it with your desired menu ID
// Replace `YOUR_MENU_ID` with the ID of the menu you want to add the categories to.
add_categories_to_menu( YOUR_MENU_ID );
How This Works:
- Sort Categories: The function sorts the categories by parent and subcategories, storing them in an array.
- Sort Categories: The function sorts the categories by parent and subcategories, storing them in an array.
- Add to Menu: It then loops through the sorted categories and adds both parent categories and their subcategories to the specified menu.
How to Use:
Find the Menu ID: Go to the WordPress Admin panel under Appearance > Menus, create a new menu or use an existing one, and get the ID of the menu.
Replace YOUR_MENU_ID: In the function above, replace YOUR_MENU_ID with the actual ID of your menu.
Save and Refresh: Save the changes in the functions.php file, and the menu should update automatically with properly aligned categories and subcategories.
This should save you time by automating the alignment of categories and subcategories under their parents without needing to manually adjust each item.