doucan8246326
2013-08-13 08:00
浏览 51
已采纳

是否可以在WordPress中使用没有链接的标题创建自定义菜单?

We are building a theme on WordPress 3.5.1 and we created two custom menus - one for the header and one for the footer. In the footer the titles are not linkable, therefore we created custom links with href "#" and then changed the href to "". The result are empty links with <a>. I know it's possible to change the cursor of these empty links with CSS:

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

and I also found out a way to remove these empty links with JavaScript and 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);
    }
});

(the footer menu is inside the .footer-content #sitemap elements:

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

)

but is it possible to remove the empty <a> elements from the HTML without using JavaScript? The function that creates the footer menu is:

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

Thanks, Uri @ Initech.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongliao4353 2013-08-13 16:03
    已采纳

    OK, I found a solution. I searched and found out function start_el(...) on class Walker_Nav_Menu, then copied it and changed only the last part of it:

    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 );
        }
    }
    

    Then I changed the call to wp_nav_menu on footer.php:

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

    It works and there are no <a> links when the href is empty (my changes are both ifs: if (! empty( $item->url )) {). Thanks to Mangesh Parte for the idea.

    Uri.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doumi4974 2013-08-13 08:55

    Yes, It is possible. You can use the walker. Put the following class in your functions.php file

    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 );
        }
    }
    

    and add this walker in your wp_nav_menu function <?php wp_nav_menu( array( 'theme_location' => 'footer_menu', 'walker' => new themeslug_walker_nav_menu ) ); ?>

    This will remove the anchor tag <a> from each menu item.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题