2017-06-30 49 views
0

我有一個通過普通的WordPress菜單(外觀>菜單)填充的側邊欄菜單。WordPress的自定義改變位置wp_nav_menu_items

我也有一個使用'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; 
} 

有沒有一種辦法實現我所追求的目標?

+0

聲明請問您的菜單有wp_query?如果是這樣,你可以簡單地將排序改爲「標題」。它會按字母順序排序。 – Dorvalla

+0

不幸的是,我沒有試圖按字母順序排列..它恰好在例子中看起來像那樣。但是,這將是一個好主意! – user1788364

回答

1

,如果我得到你的權利,那麼你需要改變你的,如果這樣的

$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++;