一个菜单的WP_NAV_MENU_ITEMS

时间:2015-09-21 作者:orèli

我测试过this tutorial. 它工作得很好。

问题是,在教程中有一个菜单,而我有两个菜单。让我知道如何定制this function 因为该功能仅适用于主菜单。

代码:

function new_nav_menu_items($items) {
    $items = "";
    $args = array(
        "post_type" => "page", 
        "order"     => "ASC", 
        "orderby"   => "menu_order"
    );
    $the_query = new WP_Query($args);

    if($the_query->have_posts()):
        while($the_query->have_posts()):
            $the_query->the_post();
                $items .= \'<li><a href="#post-\'. get_the_ID() .\'">\' . get_the_title() . \'</a></li>\';           
        endwhile;
    else:
        echo "";
    endif;

    return $items; 
}

add_filter("wp_nav_menu_items", "new_nav_menu_items");
我试过了if( $args->theme_location == \'primary\' ) 但我不知道如何使用它。。我不能申请this one

1 个回复
SO网友:Nastya

我是在自己寻找答案时偶然发现你的问题的。下面是我如何让代码为我工作的:

function new_nav_menu_items( $items, $args ) {
    if ( $args->menu == \'primary\' ) {
        $items = "";
        $args = array(
            "post_type" => "page", 
            "order"     => "ASC", 
            "orderby"   => "menu_order"
        );
        $the_query = new WP_Query( $args );
        if ( $the_query->have_posts() ):
            while( $the_query->have_posts() ): 
                $the_query->the_post();
                $items .= \'<li><a href="#post-\' . get_the_ID() . \'">\' . 
                    get_the_title() . \'</a></li>\';
            endwhile;
        else:
            echo "";
        endif;
    } 
    return $items; 
}
add_filter( "wp_nav_menu_items", "new_nav_menu_items" );