Search code examples
phpwordpressloopsmenufooter

How to loop through menu items to dynamically create a list of page links in the footer


I would like to have this menu list created dynamically in the footer:

<a href="<?php bloginfo( 'url' ); ?>/">Home</a><br>
<a href="<?php bloginfo( 'url' ); ?>/about-us/">About Us</a><br>
<a href="<?php bloginfo( 'url' ); ?>/legal-services/">Our Legal Services:</a><br>
<a class="indent" href="<?php bloginfo( 'url' ); ?>/legal-services/commercial-law/">Commercial Law</a><br>
<a class="indent" href="<?php bloginfo( 'url' ); ?>/legal-services/concrete-law/">Concrete Law</a><br>
<a class="indent" href="<?php bloginfo( 'url' ); ?>/legal-services/general-business-law/">Business Law</a><br>
<a href="<?php bloginfo( 'url' ); ?>/contact-us/">Contact Us</a>

Including being able to recognise that a menu item is a child and have it indent (eg: class="indent"). Note: This can also be coded using unordered lists.. it doesn't matter to me, as it will be a vertical list.

It is exactly the same list of pages that are in the main navigation menu bar found in the header.php

Here is the code that I have for the main navigation in the header:

<nav class="navbar navbar-inverse navbar-static-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
      <!-- button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar" -->  <!-- not on mirage? -->
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>

    <div class="navbar-collapse collapse">
        <?php 
            $args = array(
                'menu'          => 'header-menu',       
                'menu_class'    => 'nav navbar-nav navbar-right',
                'container'     => 'false'                  
            );  
            wp_nav_menu( $args );
        ?>
    </div><!--/.navbar-collapse -->
  </div>
</nav >

And what I have in the function.php for the main navigation menu:

    add_theme_support( 'menus' );

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

.

I just can't think of a way to duplicate / modify the above to do what I need in the footer. (Note this will be coded directly into footer.php, not created inside a widget.)

Can anyone offer any guidance?

EDIT:

I tried adding this code to the footer for a solution, but I must be doing something wrong, because I am getting the error message of: Menu "header-menu" not defined

          <?php 
            // Get the nav menu based on $menu_name (same as 'theme_location' or 'menu' arg to wp_nav_menu)
            // This code based on wp_nav_menu's code to get Menu ID from menu slug

            $menu_name = 'header-menu';

            if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
            $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );

            $menu_items = wp_get_nav_menu_items($menu->term_id);

            $menu_list = '<ul id="menu-' . $menu_name . '">';

            foreach ( (array) $menu_items as $key => $menu_item ) {
                $title = $menu_item->title;
                $url = $menu_item->url;
                $menu_list .= '<li><a href="' . $url . '">' . $title . '</a></li>';
            }
            $menu_list .= '</ul>';
            } else {
            $menu_list = '<ul><li>Menu "' . $menu_name . '" not defined.</li></ul>';
            }
            // $menu_list now ready to output

            echo $menu_list;
          ?>

Any help will be greatly appreciated!


Solution

  • Wow.. I actually figured it out on my own.. I was way overthinking it! The solution was so easy in the end. (Note: This is for listing pages only - to create a mini sitemap in the footer)

    You just need to add this line of code:

    <div class="footermap">
        <ul style="list-style-type: none;"><?php wp_list_pages("title_li="); ?></ul>
    </div>
    

    And add this in your css to keep the bullet points away, and line it up correctly:

    .footermap > ul {
        padding-left: 0px; 
    }
    
    .footermap > ul ul.children {
        list-style-type: none; 
        padding-left: 15px; 
    }
    

    And also be sure you clean up your list of pages in WP admin area.. don't leave any 'published' that you aren't actually using in the menu structure, because this code will pick it up... even if you don't have it marked as being added to the main navigation menu. (I just marked the ones I wanted to keep around as drafts.. just in case I needed them later eg: blog page)

    So easy to code, but it was definetly hard to find the solution!!!