Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Adding a customizable menu to your WordPress theme

Tiempo de lectura: 2 minutos
Continuing from the previous post on creating a WordPress theme (https://devcodelight.com/?p=6987), now we’re going to add a customizable menu.

To do this, we’ll go to header.php and add the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo('charset'); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php wp_title(); ?></title>
<?php wp_head(); ?>
</head>
<nav class="navbar navbar-expand-lg navbar-light bg-light mb-4">
<div class="container">
<a class="navbar-brand" href="<?php echo home_url(); ?>"><?php bloginfo('name'); ?></a>
<?php
wp_nav_menu(array(
'theme_location' => 'main-menu',
'menu_class' => 'navbar-nav ml-auto',
'container' => false,
));
?>
</div>
</nav>
<body <?php body_class(); ?>>
<!DOCTYPE html> <html <?php language_attributes(); ?>> <head> <meta charset="<?php bloginfo('charset'); ?>"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><?php wp_title(); ?></title> <?php wp_head(); ?> </head> <nav class="navbar navbar-expand-lg navbar-light bg-light mb-4"> <div class="container"> <a class="navbar-brand" href="<?php echo home_url(); ?>"><?php bloginfo('name'); ?></a> <?php wp_nav_menu(array( 'theme_location' => 'main-menu', 'menu_class' => 'navbar-nav ml-auto', 'container' => false, )); ?> </div> </nav> <body <?php body_class(); ?>>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo('charset'); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php wp_title(); ?></title>
    <?php wp_head(); ?>
</head>
<nav class="navbar navbar-expand-lg navbar-light bg-light mb-4">
    <div class="container">
        <a class="navbar-brand" href="<?php echo home_url(); ?>"><?php bloginfo('name'); ?></a>
        <?php
        wp_nav_menu(array(
            'theme_location' => 'main-menu',
            'menu_class' => 'navbar-nav ml-auto',
            'container' => false,
        ));
        ?>
    </div>
</nav>
<body <?php body_class(); ?>>

We have created the menu called main-menu:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<nav class="navbar navbar-expand-lg navbar-light bg-light mb-4">
<div class="container">
<a class="navbar-brand" href="<?php echo home_url(); ?>"><?php bloginfo('name'); ?></a>
<?php
wp_nav_menu(array(
'theme_location' => 'main-menu',
'menu_class' => 'navbar-nav ml-auto',
'container' => false,
));
?>
</div>
</nav>
<nav class="navbar navbar-expand-lg navbar-light bg-light mb-4"> <div class="container"> <a class="navbar-brand" href="<?php echo home_url(); ?>"><?php bloginfo('name'); ?></a> <?php wp_nav_menu(array( 'theme_location' => 'main-menu', 'menu_class' => 'navbar-nav ml-auto', 'container' => false, )); ?> </div> </nav>
<nav class="navbar navbar-expand-lg navbar-light bg-light mb-4">
    <div class="container">
        <a class="navbar-brand" href="<?php echo home_url(); ?>"><?php bloginfo('name'); ?></a>
        <?php
        wp_nav_menu(array(
            'theme_location' => 'main-menu',
            'menu_class' => 'navbar-nav ml-auto',
            'container' => false,
        ));
        ?>
    </div>
</nav>

And it will also allow us to customize it from WordPress itself.

Now let’s add the action that will allow us to customize it. Let’s go to functions.php and add:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function register_menus() {
register_nav_menu('main-menu', 'Main Menu');
}
add_action('after_setup_theme', 'register_menus');
function register_menus() { register_nav_menu('main-menu', 'Main Menu'); } add_action('after_setup_theme', 'register_menus');
function register_menus() {
    register_nav_menu('main-menu', 'Main Menu');
}
add_action('after_setup_theme', 'register_menus');

And now, we’ll be able to customize our menu:

We can enhance the menu design with CSS

0

Leave a Comment