在wordpress中将类添加到ul

时间:2015-03-16 02:32:47

标签: wordpress menu wordpress-theming

我是wordpress主题的新手。我正在尝试显示导航菜单

<div class="nav-collapse" id="collapse">
    <ul class="nav nav-pills">
        <li ><a href="index.php">Home</a></li>

        <li ><a href="facilities.php">Facilities</a></li>
        <li ><a href="tariff.php">Rooms &amp; Tariff</a></li>
        <li ><a href="bookings.php">Booking</a></li>
        <li ><a href="contact.php">Contact</a></li>
    </ul>
</div><!-- /.nav-collapse -->

为此,我在functions.php

中注册了菜单
function heritage_register_theme_menu() {
    register_nav_menu( 'primary', 'Main Navigation Menu' );
}
add_action( 'init', 'heritage_register_theme_menu' );

并添加了header.php中的菜单:

$defaults = array(
    'theme_location'  => '',
    'menu'            => '',
    'container'       => 'div',
    'container_class' => 'nav-collapse',
    'container_id'    => '',
    'menu_class'      => 'nav nav-pills',
    'menu_id'         => '',
    'echo'            => true,
    //'fallback_cb'     => false,
    'before'          => '',
    'after'           => '',
    'link_before'     => '',
    'link_after'      => '',
    'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
    'depth'           => 0,
    'walker'          => ''
);

wp_nav_menu( $defaults );

但在我选中的源代码中,它会将HTML显示为:

<div class="nav nav-pills">
    <ul>
        <li class="page_item page-item-9"><a href="http://localhost/projects/wordpress/?page_id=9">PHP Developer</a></li>
        <li class="page_item page-item-2"><a href="http://localhost/projects/wordpress/?page_id=2">Sample Page</a></li>
    </ul>
</div>

所以我想知道:

  1. 如何将类添加到包装div?
  2. 如何将课程nav nav-pills添加到ul而不是包装div?

1 个答案:

答案 0 :(得分:1)

您已在functions.php中注册了导航菜单,但在使用wp_nav_menu时您尚未使用该菜单。

变化:

'theme_location'  => '',

致:

'theme_location'  => 'primary',

然后你需要去外观 - &gt;如果您还没有菜单,请设置并设置菜单。