2013-01-04 42 views
1

我想拉動一些動態(非WP)的內容來填充Wordpress中的菜單。爲此,我已經延長了沃克類如下所示:WordPress的動態注入使用沃克類菜單項

http://www.kriesi.at/archives/improve-your-wordpress-navigation-menu-output

所以我的菜單將像

  • 首頁
  • 博客
  • 新聞
  • 特點
  • 比賽 -
    • 器Comp1
    • 組合物2

哪裏器Comp1和COMP2是從DB拉在一個單獨的網站。他們只是鏈接到外部網站,所以唯一相關的值是「比較標題」和「比較URL」

我的類中的主要方法是:

function start_el(&$output, $item, $depth, $args) 
    { 

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

     $class_names = $value = ''; 

     $classes = empty($item->classes) ? array() : (array) $item->classes; 

     $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item)); 
     $class_names = ' class="'. esc_attr($class_names) . '"'; 

     $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>'; 

     $attributes = ! empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) .'"' : ''; 
     $attributes .= ! empty($item->target)  ? ' target="' . esc_attr($item->target ) .'"' : ''; 
     $attributes .= ! empty($item->xfn)  ? ' rel="' . esc_attr($item->xfn  ) .'"' : ''; 
     $attributes .= ! empty($item->url)  ? ' href="' . esc_attr($item->url  ) .'"' : ''; 

     $prepend = '<strong>'; 
     $append = '</strong>'; 
     $description = ! empty($item->description) ? '<span>'.esc_attr($item->description).'</span>' : ''; 

     if($depth != 0) 
     { 
      $description = $append = $prepend = ""; 
     } 

     if($item->title == 'Competitions') 
     { 
      $item_output = $args->before; 
      $item_output .= '<a'. $attributes .'>'; 
      $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID).$append; 
      $item_output .= $description.$args->link_after; 
      $item_output .= '</a>'; 
      $item_output .= $args->after; 

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

      $this->loopComps($output , $args); 

     } 
     else 
     { 
      $item_output = $args->before; 
      $item_output .= '<a'. $attributes .'>'; 
      $item_output .= $args->link_before .$prepend.apply_filters('the_title', $item->title, $item->ID).$append; 
      $item_output .= $description.$args->link_after; 
      $item_output .= '</a>'; 
      $item_output .= $args->after; 

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

,然後loopComps方法: (getGalleryComps()的動態內容拉)

function loopComps($output , $args) 
    { 
     $openComps = $this->getGalleryComps(); 
     foreach($openComps as $comp) 
     { 
      $item = new StdClass; 
      $item->ID = 9999; 
      $item->post_author = 5; 
      $item->post_date = '2012-11-16 10:48:44'; 
      $item->post_date_gmt = '2012-11-16 10:48:44'; 
      $item->title = $comp['competition_name']; 
      $item->post_type = 'nav_menu_item'; 
      $item->post_name = $comp['competition_name']; 
      $item->post_title = $comp['competition_name']; 
      $item->post_excerpt = $comp['competition_name']; 
      $item->guid = $comp['competition_name']; 
      $item->url = 'http://www.mycomps'; 
      $item->post_status = 'publish'; 
      $item->post_parent = 0; 
      $item->filter = 'raw'; 
      $item->menu_item_parent = '6845'; 
      $item->object_id = '99999'; 
      $item->object = 'custom'; 
      $item->type = 'custom'; 
      $item->classes = array(null , 'menu-item' , 'menu-item-type-custom' , 'menu-item-object-custom'); 
      $item->menu_order = 6; 


      return $this->start_el($output, $item, 1, $args); 


     } 

    } 

這一切似乎做工精細,預計$項目未安裝到實際的菜單。如果我在導航菜單循環中打印出$ items,我可以看到我創建的'psuedo Post'的動態內容在那裏,它永遠不會附加到菜單上。

有沒有更簡單的注射菜單項的方法?

更新1:

我已經調整loopComps方法是:

function loopComps($output) 
    { 
     $openComps = $this->getGalleryComps(); 

     $output .= '<ul>'; 
     foreach($openComps as $openComp) 
     { 
      $output .= '<li><a href='.$openComp->url.'>'.$openComp['competition_name]'].'</a></li>'; 
     } 
     $output .= '</ul>'; 

     return $output; 
    } 

由於這確實讓更多的意義,但並沒有解決問題,數據是存在的,但從未在菜單中顯示。

回答

1

你應該只需要追加HTML對於比賽的菜單項,$output,例如,

$output .= '<ul>'; 
foreach($comps as $comp) { 
    $output .= '<li><a href='.$comp->url.'>.$comp->title.'</a></li>'; 
} 
$output .= '</ul>'; 

這就是爲什麼$輸出通過引用(「&」)傳遞給start_el()

這個例子可能是簡單的,因爲你必須調整它到WP生成的菜單項遵循的任何原型,但它說明了原理。創建模擬WordPress項目並試圖讓WordPress爲你做這項工作似乎並不是正確的方法。

+0

這樣做更有意義,但我仍然有同樣的問題。 $輸出從不顯示在菜單中。 – BobFlemming

1

我不知道我是否正確地理解你的問題,但既然你問Is there an easier way of injecting menu items?,所以我覺得你可以添加額外的菜單項容易使用wp_nav_menu_items鉤狀

add_filter('wp_nav_menu_items', 'your_custom_menu_item', 10, 2); 
function your_custom_menu_item ($items, $args) { 
    // First get the data from your database and replace menu titles and links 
    // then loop the result and add items 
    $myMenu='<li><a href="#">External Links</a><ul>'; // parent 
    $myMenu.='<li><a target="_blank" href="http://facebook.com">Facebook</a></li>'; 
    $myMenu.='<li><a target="_blank" href="http://google.com">Google</a></li></ul></li>'; 
    $items .= $myMenu; 
    return $items; 
} 

在剛粘貼代碼,您functions.php和替換您的菜單標題並與您的鏈接。

0

在回答問題和回答後,我在'競賽'父菜單下添加了'GOOGLE'子菜單項,所以這裏是編輯菜單欄的完整解決方案。

  • 首先把這個代碼,其中菜單欄被稱爲主要應該是在header.php文件,改變我在評論

    class description_walker extends Walker_Nav_Menu 
    { 
        function start_el(&$output, $item, $depth, $args){ 
        global $wp_query; 
        $indent = ($depth) ? str_repeat("\t", $depth) : ''; 
        $class_names = $value = ''; 
        $classes = empty($item->classes) ? array() : (array) $item->classes; 
        $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item)); 
        $class_names = ' class="'. esc_attr($class_names) . '"'; 
        $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>'; 
        $attributes = ! empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) .'"' : ''; 
        $attributes .= ! empty($item->target)  ? ' target="' . esc_attr($item->target ) .'"' : ''; 
        $attributes .= ! empty($item->xfn)  ? ' rel="' . esc_attr($item->xfn  ) .'"' : ''; 
        $attributes .= ! empty($item->url)  ? ' href="' . esc_attr($item->url  ) .'"' : ''; 
        $prepend = '<strong>'; 
        $append = '</strong>'; 
        $description = ! empty($item->description) ? '<span>'.esc_attr($item->description).'</span>' : ''; 
        if($depth != 0){ 
        $description = $append = $prepend = ""; 
        } 
        if($item->title=='Competitions'){ 
         $item_output = $args->before; 
         $item_output .= '<a'. $attributes .'>'; 
         $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID).$append; 
         $item_output .= $description.$args->link_after; 
         $item_output .= '</a>'; 
         $item_output .= $args->after; 
         /* YOUR CUSTOM MENU OR SUBMENU -START- */ 
         $output .= '<ul class="sub-menu">'; 
         $output .= '<li><a href='.'http://www.google.com'.'>'.'GOOGLE'.'</a></li></ul>'; 
         /* YOUR CUSTOM MENU OR SUBMENU -END- */ 
         $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args); 
        }else{ 
         $item_output = $args->before; 
         $item_output .= '<a'. $attributes .'>'; 
         $item_output .= $args->link_before .$prepend.apply_filters('the_title', $item->title, $item->ID).$append; 
         $item_output .= $description.$args->link_after; 
         $item_output .= '</a>'; 
         $item_output .= $args->after; 
         $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args); 
        } 
        } 
    } 
    
    • 2所提到的自定義菜單 調用wp_nav_menu並傳遞我們剛創建的自定義walker對象。

    wp_nav_menu(array( 'container' =>false, 'menu_class' => 'menuPrincipal', 'echo' => true, 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'depth' => 0, 'walker' => new description_walker()) );

它會在比賽菜單中添加GOOGLE子菜單

第一個答案是正確的,但類成員的輸出不能由loopComps設置,也是我們不應該返回輸出只是爲了設置輸出成員,所以我們只是設置直接子菜單給沃克。

它工作正常。