How to Properly Wrap Sub-Menu Elements in WordPress with Div Tags

I am currently working on styling my WordPress sub menu list items using HTML. I have written the HTML code that I want to use, but I am not sure how to incorporate it into the wp_nav_menu() function. Can you assist me with this?

<ul class="menu">
  <li class="has_children"><a>Games</a>
    <ul class="sub-menu">
      <div class="column"> <!-- this line needs adding -->
        <li>
          <h4>Games</h4>
          <ul>
            <li><a href="#">Game 1</a></li>
            <li><a href="#">Game 2</a></li>
          </ul>
        </li>
    </ul>
  </li>
</ul>

Answer №1

  1. Create a custom function

    class Custom_Nav_Pointers extends Walker_Nav_Menu
    {
        function start_lvl( &$output, $depth = 0, $args = array() )
        {
            $indent = str_repeat("\t", $depth);
            $output .= "\n$indent<ul class=\"sub-menu\">\n";
            $output .= "\n<div class=\"column\">\n";
        }
        function end_lvl( &$output, $depth = 0, $args = array() )
        {
            $indent = str_repeat("\t", $depth);
            $output .= "$indent</ul>\n".($depth ? "$indent</div>\n" : "");
        }
    }
    
  2. Incorporate this custom function into your navigation menu by adding it as an argument:

    <?php
     $navArgs = array('walker' => new Custom_Nav_Pointers());
     wp_nav_menu($navArgs);
    ?>
    

Answer №2

To begin, you will need to access your theme's functions.php file. Once there, insert the following code:

function register_my_menu() {
  register_nav_menu('header-menu',__( 'Header Menu' ));
}
add_action( 'init', 'register_my_menu' );

By adding this code, two menu options will be created: header menu and extra menu.

function register_my_menus() {
  register_nav_menus(
    array(
      'header-menu' => __( 'Header Menu' ),
      'extra-menu' => __( 'Extra Menu' )
    )
  );
}
add_action( 'init', 'register_my_menus' );

After inserting the code, these menu options should appear under the "Pages" widget in the Menu management page, allowing users to easily add them to their custom menu.

Adding Menus to Your Theme

<?php wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); ?>

Accessing the Menus Panel

Head to the Appearance -> Menus panel in your site admin dashboard. Here, you can use the GUI menu creator to assemble and customize your menu(s).

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to implement jQuery CSS hover effect using jQuery?

I've configured the hover opacity in my CSS, .button-simple:hover { opacity:.70; filter:alpha(opacity=70); filter: "alpha(opacity=70)"; } However, the hover opacity is not displaying after adding this line to my code, $('input[type ...

Variations between <div/> and <div></div>

When using Ajax to load two divs, I discovered an interesting difference in the way they are written. If I write them like this: <div id="informCandidacyId"/> <div id="idDivFiles"/> Although both divs are being loaded, only one view is added ...

Web Audio API functions are encountering a playback issue on iOS 13.3, despite working smoothly on previous iOS versions

I have been developing an audio visualizer using the web audio API. It was functioning smoothly on PC, however, after upgrading to iOS 13.3, it no longer operates on Apple mobile devices. The root cause of this issue remains a mystery to me. The problem s ...

Organizing an array of objects within MUI cards based on social media platforms

I'm currently developing an application that showcases athlete data in a grid layout using MUI Grid. The colored borders on the left side of each card are determined by the corresponding social network associated with that athlete. https://i.sstatic. ...

What steps should I take to ensure my three.js project is compatible with iPhone testing?

I recently developed a new AR project that allows users to interact with AR content through their phone camera on a website. However, I am facing an issue where I cannot easily test my website on an iPhone whenever I make changes to the code. Currently, ...

jQuery is failing to properly render dynamic content data identifiers

Need help with a dynamic HTML div <a data-id="17" onclick="getcustomer();"> <div class="note note-success"> <h4 class="block">A</h4> <p>Email : <a href="/cdn-cgi/l/email-protection" class="__cf_email__" ...

Issue with enqueuing Bootstrap js in WordPress

Why isn't my bootstrap.min.js file being included in the queue, while my bootstrap.min.css file is? Take a look at my code below: <?php // Load custom jQuery function load_custom_jquery() { wp_deregister_script('jquery'); ...

Efficient creation of a user-friendly interface application designed to retrieve data from a nearby server

The Challenge I am faced with the task of creating a simple GUI application that can either be run on a local Ubuntu 14 server or locally while still being able to access data from the server for multiple users to make changes to a basic array data file. ...

Adjusting the background color of <tr> depending on the number of <tr> elements in the HTML using CSS

When working on my HTML page, I have utilized multiple <tr> tags with different bgcolor values such as: <tr bgcolor="#000000"> <tr bgcolor="#E5F1CC"> <tr bgcolor="#D30A0A"> <tr bgcolor="#656766"> I am aiming to assign unique ...

How can I create a reverse animation using CSS?

Is there a way to reverse the animation of the circular notification in the code provided? I want the circle to move forward, covering up the info and fading out. I've tried switching the animation around but haven't had success. Any suggestions? ...

Is there a way to specifically target CSS media queries if the device resolution is similar, but the device screen size (measured in inches) varies

I possess two distinct gadgets. 1. T2 light - LCD : 15,6“ Resolution : 1920 × 1080 Device pixel ratio : 1.4375 CSS media query target : width = 1336px 2. T2 mini - LCD : 11,6“ Resolution : 1920 × 1080 Device pixel ratio : 1.4375 CSS media query t ...

Adjust the visual presentation based on the chosen option at the top of a column JQchart

Can anyone offer assistance with creating three radio button fields that will display yearly, monthly, or weekly data on a column chart? I have searched through numerous examples in JQchart but haven't found one for this specific scenario. Are there o ...

The dynamic combination of Wordpress, Ajax page loading, Gravity forms, and Gravity forms' unique page breaking feature

Currently, I have this setup: Running a WordPress site and utilizing a similar approach to , where every page content gets loaded through ajax into a <main></main> tag. On one specific page (contact page), there is a gravity form with a page ...

Swap out a portion of HTML content with the value from an input using JavaScript

I am currently working on updating a section of the header based on user input from a text field. If a user enters their zip code, the message will dynamically change to: "GREAT NEWS! WE HAVE A LOCATION IN 12345". <h4>GREAT NEWS! WE HAVE A LOCATIO ...

Pressing the icon will trigger a top-sliding dropdown mobile menu to appear

How can I ensure my mobile dropdown menu slides in from the top when the user clicks the "header-downbar-menu" icon and slides out to the top when clicked again? Currently, the button only shows the menu but I am struggling with writing the JavaScript for ...

How can I add a label to a button group created with Bootstrap?

The Nu HTML checker is throwing an error The value of the for attribute of the label element must be the ID of a non-hidden form control. <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> & ...

Is there a way to show a loading icon during the image change process without using jQuery?

How can I add a loading icon to an image change event without using jQuery? I'm new to the coding world and Stack Overflow community, so please bear with me. I've been searching for a solution to my specific situation but haven't been able ...

Deactivating a form field depending on a selected radio button in Angular 2

If I have two radio buttons, with a click function called localClick for the first button to give value 1 and the second button to give value 2. <div class="ui-g-12"><p-radioButton name="group1" value="Local" (click)=localClick(1) label="Local"&g ...

Parallax Effect Slows Down When Scrolling In Web Page

Currently in the process of creating a website with a scrolling parallax effect using Stellar.js on the header and three other sections. However, I'm experiencing lag when scrolling, especially at the top of the page. I've attempted to reduce la ...

"My JavaScript code for toggling visibility is not functioning properly. Any suggestions on how to fix

I am currently working on a task that involves showing and hiding container1, but I am confused as to why my code is not functioning properly. The task requirements are as follows: - Clicking on the "Show" button should display container1 - Clicking on the ...