2013-03-04 10 views
0

我有一個使用自定義的學步車,其輸出的導航菜單循環:如何檢查是否某一對象ID是一個網頁或交(從頁/職位本身以外)

<ul> 
<li id="menu-item-2" class="menu-item-object-category">stuff</li> 
<li id="menu-item-3" class="menu-item-object-category">stuff</li> 
... 
etc 
And if a page is in the menu, it outputs: 
<li id="menu-item-4" class="menu-item-object-page">stuff</li> 
</ul> 

顯然WordPress的地方檢測到菜單中的條目是一個類別或一個頁面並分配相應的類。我怎樣才能在我的步行者身上運行同樣的支票?

我只想做一個

If ($item->object_id = page) { // special code} 

我試過is_page(),但後來發現這是一個布爾函數來檢測,如果當前頁面你是一個網頁或沒有。

是否有任何簡單的方法來檢查每個輸出在沃克的頁面?

這是我的漫步者代碼:

class Custom_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  ) .'"' : ''; 

     $item_output = $args->before; 

       $item_output .= '<a'. $attributes .'>'; 
     $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after; 

     $item_output .= '</a>'; 
     $item_output .= $args->after; 

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

    } 
} 

感謝

回答

0

可以使用get_post功能並檢查類型,例如:

<?php 

$current_post = get_post($current_post_id); 
if ($current_post->type == 'post') { 
    //Do the stuff with post 
} 
elseif ($current_post->type == 'page') { 
    //Do the stuff with page 
} 
else { 
    //Do the stuff with other post types 
} 
+0

它需要如果($ current_post- > post_type =='page')但除此之外,這正是我需要的,謝謝! – user1202292 2013-03-04 19:52:54

相關問題