【问题标题】:WordPress change position of custom wp_nav_menu_itemsWordPress 更改自定义 wp_nav_menu_items 的位置
【发布时间】:2017-12-04 09:40:46
【问题描述】:

我有一个侧边栏菜单,通过普通的 WordPress 菜单(外观 > 菜单)填充。

我还有一个使用“wp_nav_menu_items”创建的自定义菜单,该菜单被添加到 WP 菜单的底部。我的问题是我需要能够更改该自定义菜单的顺序。这是它目前的样子:

  • 苹果
  • 香蕉
  • 橙子
  • 菠萝(使用 wp_nav_menu_items 的自定义菜单)

我想要达到的目标:

  • 苹果
  • 香蕉
  • 菠萝(使用 wp_nav_menu_items 的自定义菜单)
  • 橙子

这是我的代码目前的样子:

class My_Walker_Nav_Menu extends Walker_Nav_Menu {
    function start_lvl(&$output, $depth = 0, $args = Array()) {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<ul class=\"childnav\">\n";
    }
    function end_lvl(&$output, $depth = 0, $args = Array()) {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul>\n";
    }
}

add_filter( 'wp_nav_menu_items', 'custom_pineapple_navigation', 10, 2 );
function custom_buy_navigation ( $items, $args ) {
    $your_query = new WP_Query( 'pagename=pineapples' );
    while ( $your_query->have_posts() ) : $your_query->the_post();
        $items .= '<li class="haschild"><a href="#">Pineapple</a>                 
            <ul class="childnav">
                <li class="breadcrumb"><a href="#">Back to main menu</a></li>
                <li class="label"><a href="#">Pineapple</a></li><span class="scrollMenu scrollbar-outer">';
                    $customPosts = get_field('my_pineapple_custom_field');
                    if( $customPosts ): 
                        foreach( $customPosts as $customPost): 
                            setup_postdata($customPost);
                            $items .= '<li><a href="'.get_permalink($customPost->ID).'">'.get_the_title($customPost->ID).'</a></li>';                                                 
                        endforeach; 
                        wp_reset_postdata();
                    endif;
                $items .= '</span></ul></li>';   
    endwhile;
    wp_reset_postdata();

    // More Queries like above

    return $items;
}

有没有办法实现我的目标?

【问题讨论】:

  • 你的菜单有 wp_query 吗?如果是这样,您可以简单地将排序更改为“标题”。然后它将按字母顺序排序。
  • 不幸的是,我没有尝试按字母顺序排序.. 只是碰巧在示例中看起来像这样哈。但这本来是个好主意!

标签: php wordpress wp-nav-walker wp-nav-menu-item


【解决方案1】:

如果我说对了,那么你需要像这样更改你的 if 语句

  $i = 0;
if( $customPosts ): 
foreach( $customPosts as $customPost): 
if($i == 2) {
    echo '<a href="#">Pineapple</a>';
} else {
    setup_postdata($customPost);
    $items .= '<li><a href="'.get_permalink($customPost->ID).'">'.get_the_title($customPost->ID).'</a></li>';                                                 
endforeach; }
$i++;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-11
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    相关资源
    最近更新 更多