我想删除wp_NAV_MENU之后的最后一个。

时间:2013-07-06 作者:Nessie

我的代码调用菜单:

$defaults = array(
    \'theme_location\'  => \'\',
    \'menu\'            => \'\',
    \'container\'       => \'div\',
    \'container_class\' => true,
    \'container_id\'    => \'btn\',
    \'menu_class\'      => true,
    \'menu_id\'         => \'\',
    \'echo\'            => true,
    \'fallback_cb\'     => \'wp_page_menu\',
    \'before\'          => \'\',
    \'after\'           => \'<li class="btn_bar"><img width="18" height="38" src="http://localhost/client/fzsytech/wp-content/themes/fzsytech/img/pic-btn_bar.jpg"></li>\',
    \'link_before\'     => \'\',
    \'link_after\'      => \'\',
    \'items_wrap\'      => \'<ul>%3$s</ul>\',
    \'depth\'           => 0,
    \'walker\'          => \'\'
);

wp_nav_menu( $defaults );

?>
输出时的代码:

<ul>
    <li id="menu-item-xx" class="blah blah">ABC</li>
    <li class="btn_bar"><img width="18" height="38" src="img/pic-btn_bar.jpg"></li>
    <li id="menu-item-xx" class="blah blah">DEF</li>
    <li class="btn_bar"><img width="18" height="38" src="img/pic-btn_bar.jpg"></li>
我要删除<li class="btn_bar"><img width="18" height="38" src="img/pic-btn_bar.jpg"></li> 在最后一个菜单中?

3 个回复
SO网友:s_ha_dum

这将替换上次出现的after 一串

function replace_last_nav_item($items, $args) {
  return substr_replace($items, \'\', strrpos($items, $args->after), strlen($args->after));
}
add_filter(\'wp_nav_menu\',\'replace_last_nav_item\',100,2);
纯CSS也可以做到这一点:

.btn_bar:last-child{display:none}

SO网友:Shahinul Islam
$after = \'<li class="btn_bar"><img width="18" height="38" src="http://localhost/client/fzsytech/wp-content/themes/fzsytech/img/pic-btn_bar.jpg"></li>\';
        $defaults = array(
                \'theme_location\'  => \'primary\',
                \'menu\'            => \'\',
                \'container\'       => \'div\',
                \'container_class\' => true,
                \'container_id\'    => \'btn\',
                \'menu_class\'      => true,
                \'menu_id\'         => \'\',
                \'echo\'            => false,
                \'fallback_cb\'     => \'wp_page_menu\',
                \'before\'          => \'\',
                \'after\'           => $after,
                \'link_before\'     => \'\',
                \'link_after\'      => \'\',
                \'items_wrap\'      => \'<ul>%3$s</ul>\',
                \'depth\'           => 0,
                \'walker\'          => \'\'
            );

            $menu = wp_nav_menu( $defaults );  
            echo substr($menu, 0, strripos($menu, $after)).\'</ul>\';
SO网友:demo test

添加此代码函数。php

function replace_last_nav_item($items, $args) {
  return substr_replace($items, \'\', strrpos($items, $args->after), strlen($args->after));
}
add_filter(\'wp_nav_menu\',\'replace_last_nav_item\',100,2);
添加此代码菜单

<?php
$defaults = array(
   \'menu\'            => \'footer-menu\',
  \'container\'       => \'ul\',
  \'container_class\' => \'\',
  \'container_id\'    => \'\',
  \'menu_class\'      => \'\',
  \'menu_id\'         => \'\',
  \'echo\'            => true,
  \'fallback_cb\'     => false,
  \'before\'          => \'\',
  \'after\'           => \'&nbsp;&nbsp;&nbsp;|&nbsp;\',
  \'link_before\'     => \'\',
  \'link_after\'      => \'\',
  \'items_wrap\'      => \'<ul>%3$s</ul>\',
  \'depth\'           => 0,
  \'walker\'          => \'\'
  );
  wp_nav_menu( $defaults ); ?>

结束

相关推荐

Custom menus not showing

作为我上一次关于菜单的未解决查询的后续,这个问题已经进一步扩展。我的菜单没有打印代码中的任何地方。我正在注册菜单功能。php:add_action( \'after_setup_theme\', \'your_newtheme_setup\' ); if ( ! function_exists( \'your_newtheme_setup\' ) ) : function your_newtheme_setup() { if (