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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献