dongmi1941 2014-12-16 03:48
浏览 53
已采纳

如何遍历菜单项以在页脚中动态创建页面链接列表

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!

  • 写回答

1条回答 默认 最新

  • dongxi5505 2014-12-16 06:24
    关注

    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!!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀