{"id":215,"date":"2020-06-29T12:25:32","date_gmt":"2020-06-29T12:25:32","guid":{"rendered":"https:\/\/codexjunction.com\/?p=215"},"modified":"2023-10-29T16:17:00","modified_gmt":"2023-10-29T16:17:00","slug":"add-custom-navigation-menu-to-wordpress-theme","status":"publish","type":"post","link":"https:\/\/codexjunction.com\/add-custom-navigation-menu-to-wordpress-theme\/","title":{"rendered":"Add Custom Navigation Menu to WordPress Theme"},"content":{"rendered":"<p data-sourcepos=\"1:1-1:94\">To add a custom navigation menu to a WordPress theme through code, you can follow these steps:<\/p>\n<ol data-sourcepos=\"3:1-4:0\">\n<li data-sourcepos=\"3:1-4:0\"><strong>Register your menu location.<\/strong>\u00a0This is done by adding the following code to your theme&#8217;s\u00a0<code>functions.php<\/code>\u00a0file:<\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<div class=\"code-block-decoration header gmat-subhead-2 ng-star-inserted\">PHP<\/div>\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">my_custom_menu<\/span>() <\/span>{\r\n  register_nav_menu(<span class=\"hljs-string\">'my-custom-menu'<\/span>, <span class=\"hljs-string\">'My Custom Menu'<\/span>);\r\n}\r\nadd_action(<span class=\"hljs-string\">'init'<\/span>, <span class=\"hljs-string\">'my_custom_menu'<\/span>);\r\n<\/code><\/pre>\n<\/div>\n<p data-sourcepos=\"12:1-12:130\">This will register a new menu location called <code>my-custom-menu<\/code>. You can change the name of the menu location to anything you want.<\/p>\n<ol start=\"2\" data-sourcepos=\"14:1-23:0\">\n<li data-sourcepos=\"14:1-15:0\">\n<p data-sourcepos=\"14:4-14:181\"><strong>Create your menu.<\/strong> Go to <strong>Appearance<\/strong> &gt; <strong>Menus<\/strong> in your <a href=\"https:\/\/wordpress.com\/hosting\/?aff=15767&amp;cid=1654213&amp;cmp_id=11549382845&amp;adg_id=111353876614&amp;kwd=wordpress&amp;device=c&amp;gclid=Cj0KCQjwhfipBhCqARIsAH9msblrK3gNeJTdcJc3ATfOOpPTU7aZp9Ft75DFJY0C4NFMKI7lfVf9c9QaAqjXEALw_wcB\" rel=\"nofollow\">WordPress<\/a> dashboard and click the <strong>Create New Menu<\/strong> button. Enter a name for your menu and click <strong>Create Menu<\/strong>.<\/p>\n<\/li>\n<li data-sourcepos=\"16:1-17:0\">\n<p data-sourcepos=\"16:4-16:195\"><strong>Add menu items to your menu.<\/strong> You can add pages, posts, categories, and other links to your menu. To add an item, click the <strong>Add Items<\/strong> button and select the type of item you want to add.<\/p>\n<\/li>\n<li data-sourcepos=\"18:1-19:0\">\n<p data-sourcepos=\"18:4-18:199\"><strong>Arrange your menu items.<\/strong> Once you have added all of your menu items, you can arrange them in the order that you want them to appear. To do this, drag and drop the items into the desired order.<\/p>\n<\/li>\n<li data-sourcepos=\"20:1-21:0\">\n<p data-sourcepos=\"20:4-20:207\"><strong>Assign your menu to the menu location.<\/strong> Click the <strong>Save Menu<\/strong> button. Then, click the <strong>Manage Locations<\/strong> tab and select the <code>my-custom-menu<\/code> location from the dropdown menu. Click <strong>Save Changes<\/strong>.<\/p>\n<\/li>\n<li data-sourcepos=\"22:1-23:0\">\n<p data-sourcepos=\"22:4-22:139\"><strong>Add the menu code to your theme template file.<\/strong> Open the template file where you want your menu to appear and add the following code:<\/p>\n<\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<div class=\"code-block-decoration header gmat-subhead-2 ng-star-inserted\">PHP<\/div>\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-meta\">&lt;?php<\/span> wp_nav_menu( <span class=\"hljs-keyword\">array<\/span>( <span class=\"hljs-string\">'theme_location'<\/span> =&gt; <span class=\"hljs-string\">'my-custom-menu'<\/span> ) ); <span class=\"hljs-meta\">?&gt;<\/span><\/code><\/pre>\n<\/div>\n<p data-sourcepos=\"28:1-28:90\">Replace <code>my-custom-menu<\/code> with the name of the menu location that you registered in step 1.<\/p>\n<ol start=\"7\" data-sourcepos=\"30:1-31:0\">\n<li data-sourcepos=\"30:1-31:0\"><strong>Save your theme template file and upload it to your WordPress site.<\/strong><\/li>\n<\/ol>\n<p data-sourcepos=\"32:1-32:105\">Once you have completed these steps, your custom navigation menu will appear in your theme template file.<\/p>\n<p data-sourcepos=\"34:1-34:12\"><strong>Example:<\/strong><\/p>\n<p data-sourcepos=\"36:1-36:114\">To add a custom navigation menu to your theme&#8217;s header, you would add the following code to the <code>header.php<\/code> file:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<div class=\"code-block-decoration header gmat-subhead-2 ng-star-inserted\">PHP<\/div>\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-meta\">&lt;?php<\/span> wp_nav_menu( <span class=\"hljs-keyword\">array<\/span>( <span class=\"hljs-string\">'theme_location'<\/span> =&gt; <span class=\"hljs-string\">'my-custom-menu'<\/span> ) ); <span class=\"hljs-meta\">?&gt;<\/span>\r\n<\/code><\/pre>\n<\/div>\n<p data-sourcepos=\"42:1-42:174\">Assuming that you have registered a menu location called <code>my-custom-menu<\/code> and created a menu called &#8220;My Custom Menu&#8221;, this code will display that menu in your theme&#8217;s header.<\/p>\n<p data-sourcepos=\"44:1-44:20\"><strong>Additional tips:<\/strong><\/p>\n<ul data-sourcepos=\"46:1-49:0\">\n<li data-sourcepos=\"46:1-46:80\">You can add multiple menus to your theme and register multiple menu locations.<\/li>\n<li data-sourcepos=\"47:1-47:107\">You can customize the appearance of your navigation menu by adding CSS styles to your theme&#8217;s stylesheet.<\/li>\n<li data-sourcepos=\"48:1-49:0\">You can also use plugins to add more features to your WordPress navigation menus, such as mega menus and sticky menus.<\/li>\n<\/ul>\n<p data-sourcepos=\"50:1-50:198\">If you are having trouble adding a custom navigation menu to your WordPress theme through code, you can consult the WordPress documentation or contact your <a href=\"https:\/\/codexjunction.com\/when-do-you-really-need-managed-wordpress-hosting\/\">WordPress hosting<\/a> provider for assistance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To add a custom navigation menu to a WordPress theme through code, you can follow these steps: Register your menu location.\u00a0This is done by adding the following code to your theme&#8217;s\u00a0functions.php\u00a0file: PHP function my_custom_menu() { register_nav_menu(&#8216;my-custom-menu&#8217;, &#8216;My Custom Menu&#8217;); } add_action(&#8216;init&#8217;, &#8216;my_custom_menu&#8217;); This will register a new menu location called my-custom-menu. You can change the name of the menu location to anything you want. Create your menu. Go to Appearance &gt; Menus in your WordPress dashboard and click the Create New Menu button. Enter a name for your menu and click Create Menu. Add menu items to your menu. You [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":879,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[512,246,508,510,511,509],"class_list":["post-215","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-a-online-course-web-tutorial","tag-custom-navigation-menu","tag-learn-java-online","tag-learn-laravel-online","tag-learn-php-online","tag-learn-python-online"],"_links":{"self":[{"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/comments?post=215"}],"version-history":[{"count":5,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":2643,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/posts\/215\/revisions\/2643"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/media\/879"}],"wp:attachment":[{"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}