{"flag":true,"single":true,"pageTitle":"Replace category-based dynamic menu items with custom post type posts in WordPress nav menu","post":{"id":345,"user_id":"1","slug":"replace-category-based-dynamic-menu-items-with-custom-post-type-posts-in-wordpress-nav-menu-1gbn","title":"Replace category-based dynamic menu items with custom post type posts in WordPress nav menu","body":"<pre class=\"language-markup\"><code>&lt;?php\r\n\/* add code in function.php \r\n\tadd CSS Classes (optional) &gt;&gt; class = articles-menu inside menu \r\nadd_action('init', 'my_register_category_for_article', 20);\r\n\t that's it, all categories, of custom post type articles will be added in this dropdown\r\n*\/\r\n\r\n\r\nadd_action('wp_enqueue_scripts', function() {\r\n    wp_enqueue_style(\r\n        'child-style',\r\n        get_stylesheet_uri(), \/\/ loads style.css\r\n        array(),\r\n        filemtime(get_stylesheet_directory() . '\/style.css') \/\/ cache busting\r\n    );\r\n});\r\n\r\n\r\n\r\nfunction my_article_menu_terms() {\r\n    global $wpdb;\r\n\r\n    return $wpdb-&gt;get_results(\r\n        $wpdb-&gt;prepare(\r\n            \"\r\n            SELECT DISTINCT t.term_id, t.name\r\n            FROM {$wpdb-&gt;terms} t\r\n            INNER JOIN {$wpdb-&gt;term_taxonomy} tt ON t.term_id = tt.term_id\r\n            INNER JOIN {$wpdb-&gt;term_relationships} tr ON tt.term_taxonomy_id = tr.term_taxonomy_id\r\n            INNER JOIN {$wpdb-&gt;posts} p ON tr.object_id = p.ID\r\n            WHERE tt.taxonomy = %s\r\n              AND p.post_type = %s\r\n              AND p.post_status = %s\r\n            ORDER BY t.name ASC\r\n            \",\r\n            'category',\r\n            'article',\r\n            'publish'\r\n        )\r\n    );\r\n}\r\n\r\nadd_filter('wp_nav_menu_objects', function ($items, $args) {\r\n    $terms = my_article_menu_terms();\r\n\r\n    if (empty($terms)) {\r\n        return $items;\r\n    }\r\n\r\n    $parent_index = null;\r\n\r\n    foreach ($items as $i =&gt; $item) {\r\n        $classes = is_array($item-&gt;classes) ? $item-&gt;classes : array();\r\n\r\n        if (in_array('articles-menu', $classes, true)) {\r\n            $parent_index = $i;\r\n            break;\r\n        }\r\n    }\r\n\r\n    if ($parent_index === null) {\r\n        return $items;\r\n    }\r\n\r\n    $parent = $items[$parent_index];\r\n    $next_id = 100000;\r\n\r\n    foreach ($items as $item) {\r\n        $next_id = max($next_id, (int) $item-&gt;ID + 1);\r\n    }\r\n\r\n    $children = array();\r\n\r\n    foreach ($terms as $term) {\r\n        $url = get_term_link((int) $term-&gt;term_id, 'category');\r\n\r\n        if (is_wp_error($url)) {\r\n            continue;\r\n        }\r\n\r\n        $child = new stdClass();\r\n        $child-&gt;ID = $next_id++;\r\n        $child-&gt;db_id = $child-&gt;ID;\r\n        $child-&gt;menu_item_parent = $parent-&gt;ID;\r\n        $child-&gt;object_id = (int) $term-&gt;term_id;\r\n        $child-&gt;object = 'category';\r\n        $child-&gt;type = 'taxonomy';\r\n        $child-&gt;type_label = 'Category';\r\n        $child-&gt;title = $term-&gt;name;\r\n        $child-&gt;url = $url;\r\n        $child-&gt;target = '';\r\n        $child-&gt;attr_title = '';\r\n        $child-&gt;description = '';\r\n        $child-&gt;classes = array('menu-item', 'menu-item-type-taxonomy', 'menu-item-object-category','articles_sub_menu');\r\n        $child-&gt;xfn = '';\r\n        $child-&gt;status = 'publish';\r\n        $child-&gt;current = is_category((int) $term-&gt;term_id);\r\n\r\n        $children[] = $child;\r\n    }\r\n\r\n    array_splice($items, $parent_index + 1, 0, $children);\r\n\r\n    return $items;\r\n}, 10, 2);\r\n<\/code><\/pre>","category_id":"29","is_private":"0","created_at":"2026-04-30T06:24:29.000000Z","updated_at":"2026-04-30T06:24:45.000000Z","category":{"id":29,"user_id":"1","name":"Wordpress","slug":"wordpress-uzoa","parent_id":"1","created_at":"2023-11-10T02:49:29.000000Z","updated_at":"2023-11-10T02:49:29.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"&lt;?php \/* add code in function.php  \tadd CSS Classes (optional) &gt;&gt; class = articles-menu inside menu  add_action('init', 'my_registe - Replace category-based dynamic menu items with custom post type posts in WordPress nav menu (Updated: April 30, 2026) - Read more about Replace category-based dynamic menu items with custom post type posts in WordPress nav menu at my programming site [SITE]","categories":[]}