How to fetch pages, categories, subcategories in WordPress menu

In this tutorial we will see how we can fetch pages, categories, subcategories in our menu and display in our WordPress website.

First we need to create a custom menu in WordPress.

1. Check menu link under appearance of WordPress admin panel

If the menu link is present under the appearance then you can discard the following steps.

If the menu link is not present then it means that your WordPress theme does not supports custom menus. To enable custom menu support in your theme add the following code in your functions.php file present inside your theme.

register_nav_menus( array(
 'primary_menu' => 'Main Menu',
));

The above code will add custom menu support in your WordPress theme. Now you can find menu link under appearance.

2. Create all pages, categories and subcategories in your WordPress website.

Next you can create all the pages, categories and subcategories from the administrator section of the website as per your requirement.

Create Pages, Categories and Subcategories that you want to keep in the menu of the website.

Now go to the menu under appearance in the administrator section of the website and create a menu.

You can give a name for the menu like "Primary Menu".

3. Displaying the menu in the WordPress website

After the menu is created we can display the menu in the header section of our WordPress theme.

Go to the theme folder and edit your header.php file.
In the header.php file you can add the following code.

<nav class="navigation">
                <?php
                                    wp_nav_menu( array(
                                        'theme_location' => 'primary_menu',
                                        'container' => 'none',
                                     ) );
                                ?>
 </nav><!-- End navigation -->

In the above code 'primary_menu' is the menu location identifier.

The argument container=>none in the above code tells that the menu <ul>....</ul> doesn't needs a container. The default container is <div>.

You can also create multiple menus by following the above process.


Comments

Popular posts from this blog

3 Free Multi vendor Plugins for WordPress

How to add a drop down menu in Word Press for your site?

best ways to optimize and boost SEO of your WordPress website