有可能在WordPress中创建带有标题而没有链接的自定义菜单吗


Is it possible to create custom menus in WordPress with titles without links?

我们正在WordPress 3.5.1上构建一个主题,并创建了两个自定义菜单-一个用于页眉,一个用于页脚。在页脚中,标题是不可链接的,因此我们使用href"#"创建了自定义链接,然后将href更改为"。结果是与<a>的空链接。我知道用CSS:可以更改这些空链接的光标

.footer-content #sitemap ul.menu > li.menu-item > a {
    cursor: text;
}

我还找到了一种用JavaScript和jQuery删除这些空链接的方法:

$('.footer-content #sitemap ul.menu > li.menu-item > a').each(function() {
    // If href is empty, remove <a> element.
    var href = $(this).attr('href');
    var href_length = 0;
    if (!(typeof href === 'undefined')) {
        var href_length = href.length;
    }
    if (href_length === 0) {
        var contents = $(this).contents();
        $(this).replaceWith(contents);
    }
});

(页脚菜单位于.footer-content #sitemap元素内部:

<div class="footer-content">
<div id="sitemap" class="not_mobile">

)

但是可以在不使用JavaScript的情况下从HTML中删除空的CCD_ 3元素吗?创建页脚菜单的功能是:

<?php wp_nav_menu( array( 'theme_location' => 'footer_menu' ) ); ?>

谢谢,Uri@Initech。

是的,这是可能的。您可以使用walker。在functions.php文件中放入以下类

class themeslug_walker_nav_menu extends Walker_Nav_Menu {
    // add main/sub classes to li's and links
    function start_el( &$output, $item, $depth, $args ) {
        global $wp_query;
        $indent = ( $depth > 0 ? str_repeat( "'t", $depth ) : '' ); // code indent
        // depth dependent classes
        $depth_classes = array(
            ( $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ),
            ( $depth >=2 ? 'sub-sub-menu-item' : '' ),
            ( $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ),
            'menu-item-depth-' . $depth
        );
        $depth_class_names = esc_attr( implode( ' ', $depth_classes ) );
        // passed classes
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $class_names = esc_attr( implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ) );
        // build html
        $output .= $indent . '<li id="nav-menu-item-'. $item->ID . '" class="' . $depth_class_names . ' ' . $class_names . '">';
        // link attributes
        $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
        $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
        $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
        $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
        $attributes .= ' class="menu-link ' . ( $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ) . '"';
        if($depth == 0) {
            $item_output = sprintf( '%1$s%2$s%3$s%4$s%5$s',
                $args->before,
                $args->link_before,
                apply_filters( 'the_title', $item->title, $item->ID ),
                $args->link_after,
                $args->after
            );
        } else {
            $item_output = sprintf( '%1$s<a%2$s>%3$s%4$s%5$s</a>%6$s',
                $args->before,
                $attributes,
                $args->link_before,
                apply_filters( 'the_title', $item->title, $item->ID ),
                $args->link_after,
                $args->after
            );
        }
        // build html
        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}

并将此助行器添加到您的wp_nav_menu函数中<?php wp_nav_menu( array( 'theme_location' => 'footer_menu', 'walker' => new themeslug_walker_nav_menu ) ); ?>

这将从每个菜单项中删除锚点标记<a>

好的,我找到了一个解决方案。我在类Walker_Nav_Menu上搜索并找到了函数start_el(...),然后复制了它,只更改了它的最后一部分:

class themeslug_walker_nav_menu extends Walker_Nav_Menu {
    /**
     * @see Walker::start_el()
     * @since 3.0.0
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @param object $item Menu item data object.
     * @param int $depth Depth of menu item. Used for padding.
     * @param int $current_page Menu item ID.
     * @param object $args
     */
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        $indent = ( $depth ) ? str_repeat( "'t", $depth ) : '';
        $class_names = $value = '';
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = 'menu-item-' . $item->ID;
        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
        $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
        $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
        $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
        $output .= $indent . '<li' . $id . $value . $class_names .'>';
        $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
        $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
        $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
        $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
        $item_output = $args->before;
        if (! empty( $item->url )) {
            $item_output .= '<a'. $attributes .'>';
        }
        $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
        if (! empty( $item->url )) {
            $item_output .= '</a>';
        }
        $item_output .= $args->after;
        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}

然后我在footer.php上将呼叫改为wp_nav_menu

<?php wp_nav_menu( array( 'theme_location' => 'footer_menu', 'walker' => new themeslug_walker_nav_menu ) ); ?>

它有效,并且当href为空时没有<a>链接(我的更改都是ifs:if (! empty( $item->url )) {)。感谢Mangesh Parte的创意。

Uri。