wordpress 导航调用

functions.php 写好后在后台页面添加导航

添加好以后 在header.php页面调用

<?php
    wp_nav_menu(array(
        'theme_location' => 'nav-1' //theme_location 导航的位置
    ))
?><!--前台调取导航信息-->

以下是参数

<?php
    wp_nav_menu(array(
        'theme_location' => 'nav-1', //theme_location 导航的位置
        'container' =>'nav',//外层div
        'container_class' => 'nav',//外层div的类
        'container_id' => '', //ul父节点id值
        'menu_class' => 'menu', //ul节点class值
        'menu_id' => 'menu', //ul节点id值
        'echo' => true,//是否输出菜单,默认为真
        'fallback_cb' => 'wp_page_menu', //菜单不存在时,返回默认菜单,设为false则不返回
        'before' => '<span>', //a链接前文本或各种标签
        'after' => '</span>', //a链接后文本或各种标签
        'link_before' => '前', //a链接里面->前
        'link_after' => '后', //a链接里面->后
        'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', //如何包装列表 ul ol等等
        'depth' => 0, //菜单深度,默认0
        'walker' => new WordPresskt_Nav_Walker() //调用实例化inc继承过的类
    ))
?><!--前台调取导航信息-->

下面为调用二级导航

创建inc目录

<?php
class WordPresskt_Nav_Walker extends Walker_Nav_Menu{
    /** 
     * 修改二级菜单
    */
    public function end_lvl(&$output,$depth=0,$args = array()){
        if(isset($args->item_spacing) && 'discard' === $args->item_spacing){
            $t = '';
            $n = '';
        }else{
            $t = "\t";
            $n = "\n";
        }
        $indent = str_repeat($t,$depth);
        $suffix = '<span class="fa fa-angle-down"></span>';//添加按钮对应的html
        $output .= "$indent</ul>{$suffix}{$n}";
    }
}
?>

在functions.php调用

//调用二级菜单函数
function wordpresskt_load_self_nav_walker(){
    //加载walker类
    include get_theme_file_path().'/inc/class-my-nav-walker.php';
}
add_action('after_setup_theme','wordpresskt_load_self_nav_walker');