让wordpress输出bootstrap的菜单结构

现在自适应网页(即常说的响应式设计,一个网页在PC平板手机上显示不同的布局)用的越来越多,然而,对于大多数人来说,写一个自适应的网页并非易事,于是有了bootstrap。Bootstrap是twitter的工程师利用业余时间制作推出的一个开源的用于前端开发的工具包,即里面已经写好了css js,你只需要引入它的js和css,然后根据要求,给网页的div添加相应的class属性,即可制作一个响应式网页。

说实话,bootstrap很方便,作者使用过一次,但是有一个缺点,那就是你需要引入bootstrap的框架的css和js,然而这个css里面,有很多代码都是你用不到的,这样就会产生很多冗余代码,而去除css的冗余代码绝对是个体力活,所以作者用过一次就不用了。

将bootstrap应用到wordpress上也很简单,唯一可能有困难的就是菜单,因为bootstrap的菜单有他自己的结构和属性

查看网页源文件,可以看到菜单的结构大概是这样

 <ul class="nav navbar-nav">

<li class="active"><a href="#">Link</a></li>

<li class="dropdown">

<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>

<ul class="dropdown-menu">

<li><a href="#">Action</a></li>

<li><a href="#">Another action</a></li>

</ul>

</li>

</ul>要在wordpress上实现这个菜单结构看似不难,其实,里面Dropdown后面的<b class="caret"></b>对应网页中下拉菜单的那个到三角形,以及二级菜单的ul标签的class属性。除非你把菜单写死,否则使用wp_nav_menu函数是无法输出这两个内容的。

那要怎么办呢?

wordpress的wp_nav_menu函数参数如下:

 <?php

$defaults = array(

'theme_location' => '',

'menu' => '',

'container' => 'div',

'container_class' => '',

'container_id' => '',

'menu_class' => 'menu',

'menu_id' => '',

'echo' => true,

'fallback_cb' => 'wp_page_menu',

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

?>其中的walker参数是关键。

更改你的主题菜单输出函数wp_nav_menu的walker参数:

 <?php

wp_nav_menu( array(

'theme_location' => 'ashu_menu',

'depth' => 2,

'container' => false,

'menu_class' => 'nav navbar-nav',

//添加或更改walker参数

'walker' => new wp_bootstrap_navwalker())

);

?>上面代码中walker参数的值是一个类,所以接下来你需要添加这个类,在你的主题functions.php文件中添加下面代码:

 <?php

/**

* Class Name: wp_bootstrap_navwalker

* GitHub URI: https://github.com/twittem/wp-bootstrap-navwalker

* Description: A custom WordPress nav walker class to implement the Bootstrap 3 navigation style in a custom theme using the WordPress built in menu manager.

* Version: 2.0.4

* Author: Edward McIntyre - @twittem

* License: GPL-2.0+

* License URI: http://www.gnu.org/licenses/gpl-2.0.txt

*/

class wp_bootstrap_navwalker extends Walker_Nav_Menu {

/**

* @see Walker::start_lvl()

* @since 3.0.0

*

* @param string $output Passed by reference. Used to append additional content.

* @param int $depth Depth of page. Used for padding.

*/

public function start_lvl( &$output,$depth = 0,$args = array() ) {

$indent = str_repeat( "t",$depth );

$output .= "n$indent<ul role="menu" class=" dropdown-menu">n";

}

/**

* @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

*/

public function start_el( &$output,$item,$args = array(),$id = 0 ) {

$indent = ( $depth ) ? str_repeat( "t",$depth ) : '';

/**

* Dividers,Headers or Disabled

* =============================

* Determine whether the item is a Divider,Header,Disabled or regular

* menu item. To prevent errors we use the strcasecmp() function to so a

* comparison that is not case sensitive. The strcasecmp() function returns

* a 0 if the strings are equal.

*/

if ( strcasecmp( $item->attr_title,'divider' ) == 0 && $depth === 1 ) {

$output .= $indent . '<li role="presentation" class="divider">';

} else if ( strcasecmp( $item->title,'divider') == 0 && $depth === 1 ) {

$output .= $indent . '<li role="presentation" class="divider">';

} else if ( strcasecmp( $item->attr_title,'dropdown-header') == 0 && $depth === 1 ) {

$output .= $indent . '<li role="presentation" class="dropdown-header">' . esc_attr( $item->title );

} else if ( strcasecmp($item->attr_title,'disabled' ) == 0 ) {

$output .= $indent . '<li role="presentation" class="disabled"><a href="#">' . esc_attr( $item->title ) . '</a>';

} else {

$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 ),$args ) );

if ( $args->has_children )

$class_names .= ' dropdown';

if ( in_array( 'current-menu-item',$classes ) )

$class_names .= ' active';

$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

$id = apply_filters( 'nav_menu_item_id','menu-item-'. $item->ID,$args );

$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

$output .= $indent . '<li' . $id . $value . $class_names .'>';

$atts = array();

$atts['title'] = ! empty( $item->title ) ? $item->title : '';

$atts['target'] = ! empty( $item->target ) ? $item->target : '';

$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';

// If item has_children add atts to a.

if ( $args->has_children && $depth === 0 ) {

$atts['href'] = '#';

$atts['data-toggle'] = 'dropdown';

$atts['class'] = 'dropdown-toggle';

$atts['aria-haspopup'] = 'true';

} else {

$atts['href'] = ! empty( $item->url ) ? $item->url : '';

}

$atts = apply_filters( 'nav_menu_link_attributes',$atts,$args );

$attributes = '';

foreach ( $atts as $attr => $value ) {

if ( ! empty( $value ) ) {

$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );

$attributes .= ' ' . $attr . '="' . $value . '"';

}

}

$item_output = $args->before;

/*

* Glyphicons

* ===========

* Since the the menu item is NOT a Divider or Header we check the see

* if there is a value in the attr_title property. If the attr_title

* property is NOT null we apply it as the class name for the glyphicon.

*/

if ( ! empty( $item->attr_title ) )

$item_output .= '<a'. $attributes .'><span class="glyphicon ' . esc_attr( $item->attr_title ) . '"></span>&nbsp;';

else

$item_output .= '<a'. $attributes .'>';

$item_output .= $args->link_before . apply_filters( 'the_title',$item->title,$item->ID ) . $args->link_after;

$item_output .= ( $args->has_children && 0 === $depth ) ? ' <span class="caret"></span></a>' : '</a>';

$item_output .= $args->after;

$output .= apply_filters( 'walker_nav_menu_start_el',$item_output,$depth,$args );

}

}

/**

* Traverse elements to create list from elements.

*

* Display one element if the element doesn't have any children otherwise,

* display the element and its children. Will only traverse up to the max

* depth and no ignore elements under that depth.

*

* This method shouldn't be called directly,use the walk() method instead.

*

* @see Walker::start_el()

* @since 2.5.0

*

* @param object $element Data object

* @param array $children_elements List of elements to continue traversing.

* @param int $max_depth Max depth to traverse.

* @param int $depth Depth of current element.

* @param array $args

* @param string $output Passed by reference. Used to append additional content.

* @return null Null on failure with no changes to parameters.

*/

public function display_element( $element,&$children_elements,$max_depth,$args,&$output ) {

if ( ! $element )

return;

$id_field = $this->db_fields['id'];

// Display this element.

if ( is_object( $args[0] ) )

$args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] );

parent::display_element( $element,$children_elements,$output );

}

/**

* Menu Fallback

* =============

* If this function is assigned to the wp_nav_menu's fallback_cb variable

* and a manu has not been assigned to the theme location in the WordPress

* menu manager the function with display nothing to a non-logged in user,

* and will add a link to the WordPress menu manager if logged in as an admin.

*

* @param array $args passed from the wp_nav_menu function.

*

*/

public static function fallback( $args ) {

if ( current_user_can( 'manage_options' ) ) {

extract( $args );

$fb_output = null;

if ( $container ) {

$fb_output = '<' . $container;

if ( $container_id )

$fb_output .= ' id="' . $container_id . '"';

if ( $container_class )

$fb_output .= ' class="' . $container_class . '"';

$fb_output .= '>';

}

$fb_output .= '<ul';

if ( $menu_id )

$fb_output .= ' id="' . $menu_id . '"';

if ( $menu_class )

$fb_output .= ' class="' . $menu_class . '"';

$fb_output .= '>';

$fb_output .= '<li><a href="'%20.%20admin_url(%20'nav-menus.php'%20)%20.%20'">Add a menu</a></li>';

$fb_output .= '</ul>';

if ( $container )

$fb_output .= '</' . $container . '>';

echo $fb_output;

}

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


我想将wordpress的默认接口路由改掉,愿意是默认的带一个 wp-json,我想换成 api,直接简单明了。默认路由https://www.xxzhuti.com/wp-json/wp/v2/posts/1新的路由https://www.xxzhuti.com/api/wp/v2/posts/1路由修改wordpress提供了一个钩子来修改 wp-json 的地址,代码入汛function r
wordpress自定义分类法之后,我看到链接都自动在后面添加了一个斜杠,如下图所示打开链接后都是先从一个链接301后到另外一个链接,我这个纳闷,然后一顿找原因,最后竟然是因为固定链接看我后台的固定链接是 /%post_id%/ 这么写的,然后每次都会301自动跳转。正确的写法是 /%post_id%.html 这样写才不会301跳转。一个小问题,往往一个小问题就会折腾好长时间,在这记录一下吧..
事情是这样的,我用 get_post_type 函数创建了一个自定义分类法类型,然后自定义了文件的访问路径代码如下。function tblog_chat_template( $template_path ){    if ( get_post_type() == 'aichat' ) {         if ( is_single() )         {             $temp
最近网站莫名其妙的被顶上了,过一个多小时,就注册一个账号,虽然不多,但是也挺烦,时间一长垃圾注册的就多了。由于我前台把注册关掉了,只允许用第三方登录,经过查找发现是通过后台注册的,后台无须邮箱验证即可注册,也是就想着在后台加个验证码。效果如下实现方法一将下面的代码加入到 function.php 中,刷新页面就会看到。//WordPress新用户注册随机数学验证码function add_s...
最近服务器要到期了,就想着把网站转移到另外一台服务器,本来打算要续费的,但是腾讯云的服务器有了新的规定,域名必须在腾讯云备案才能使用,在别的地方备案的域名需要转到腾讯云,在腾讯云在走一遍备案流程,若是备案简单,时间短也就无所谓了,就转一下,可是这个备案大家也知道,至少得一个月,于是就只能放弃腾讯云了,将站点转回备案的阿里云,以后就用阿里云了,贵点就贵点吧。这不网站迁移过去了,死活图片不显示,各..
今天在写wordpress的接口,然后碰到个奇怪的问题,怎么访问都提示 rest_no_route 的错误,然后就一直跟踪代码,终于找到原因所在。报错信息{'code': 'rest_no_route','message': '未找到匹配URL和请求方式的路由。','data': {'status': 404}}原因分析register_rest_route( string $namespace,
今天看到群友突然滴滴滴的找我QQ,好像出什么大事情一样。原来他由于误操作导致网站文章被删除。
今天遇到一个网友提示网站访问速度比较慢,然后通知日志看到有他的网站WordPress默认登录地址wp-login.php一直在被爬虫抓取,因为是有被破解后台账户信息,导致的占用数据库资源使得网站资源带宽不足后访问降低导致
今天老左在帮助客户升级WordPress的时候我看他网站也比较简单就直接在后台手工升级的。升级后打开网站有出现"There has been a critical error on your website."的错误提示。
我们在使用WordPress搭建的网站是否有留意查看源代码的时候看到标题中的原本"-",被转义成"&#8211"。其实这样并不影响用户阅读体验和搜索引擎,搜索引擎在收录后也会反向转义的。就是感觉看
WordPress默认是无法上传.webp格式的文件,如果要上传需要对上传文件限制进行解除后才能上传。
GTranslate 是一个流行的翻译插件,在免费和付费版本中提供不同的体验。 使用免费版本,您可以轻松提供任何语言的动态翻译,而付费版本提供可索引、可编辑的翻译版本,就像 Weglot 一样。
时下主流的浏览器都自带了广告屏蔽功能,还有ADSafe、ADB等软件插件,不断更新,屏蔽网站代码!
您是否正在寻找可用于显示数据的 WordPress 表格插件? WordPress 表格插件可让您以表格格式存储和呈现数据,以方便访问者阅读。
wordPress数据库除了本身的表以外,部分插件会增加表来存储数据。如果插件已经删除了,这些插件生成的表还在数据库中,那么怎么删除呢?
WordPress几乎是世界上最安全的开源程序,但其后台登录注册的地址始终是众所周知的,所以不少人觉得隐藏WordPress的登录地址后可能会更加安全。下面浩子就针对这个问题来简单说明一下方法。WordPress隐藏后台登录地址
自 WordPress 5.8 正式版发布 以后,后台的小工具就默认是块编辑器的界面样式,很多老用户还没有来得及学习就一脸茫然了,今天浩子就来教大家如何恢复WordPress经典小工具。
您想用您的语言翻译 WordPress 插件吗?许多 WordPress 插件是完全可翻译的,世界上任何人都可以轻松翻译。在本文中,我们将向您展示如何轻松地将 WordPress 插件翻译成您的语言(无需任何代码)。
WordPress新手不清楚WordPress调试模式怎么开启,今天来教大家认识和开启。什么是WordPress的调试模式
最近不少用户反馈说WordPress后台的外观-自定义点开后直接报错,错误信息如下: