2012-11-28 31 views
2

我正在尋找顯示在wordpress菜單中分層父帖子的子帖子數量。所以,我會將父郵件添加到菜單中。顯然,我將不得不與定製的Walker一起去,但我不確定從哪裏開始。Wordpress沃克菜單的兒童帖子數量

即:

Menu Item (8) 
Menu Item (15) 
Menu Item (7) 

回答

3

WordPress的存儲父/子菜單項通過自定義字段關係稱爲_menu_item_menu_item_parent。因此,您可以查詢下列方式子菜單項:

class add_child_numbers_walker extends Walker_Nav_Menu { 
    function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) { 
     global $wp_query; 
     $indent = ($depth) ? str_repeat("\t", $depth) : ''; 

     $class_names = ''; 

     $classes = empty($item->classes) ? array() : (array) $item->classes; 
     $classes[] = 'menu-item-' . $item->ID; 

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

     $id = apply_filters('nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args); 
     $id = $id ? ' id="' . esc_attr($id) . '"' : ''; 

     $output .= $indent . '<li' . $id . $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  ) .'"' : ''; 

     $submenus = $depth == 0 ? get_posts(array('post_type' => 'nav_menu_item', 'numberposts' => -1, 'orderby' => 'menu_order', 'order' => 'ASC', 'meta_query' => array(array('key' => '_menu_item_menu_item_parent', 'value' => $item->ID)))) : false; 

     $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 .= $submenus ? ' <span class="submenus-count">(' . count($submenus) . ')</span>' : ''; 
     $item_output .= $args->after; 

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

這個遊走將畢竟頂級鏈接添加<span class="submenus-count">(X)</span>

可以調用這樣的菜單: wp_nav_menu(array('theme_location' => 'theme-location', 'container' => '', 'walker' => new add_child_numbers_walker()));


既然你說你想要做一個自定義函數來顯示你的菜單,這裏是一個簡單的解決方案:

function my_custom_nav($post_type = 'page') { 
    $current = is_singular() ? get_the_ID() : false; 

    $posts = get_posts(array('post_type' => $post_type, 'numberposts' => -1, 'orderby' => 'menu_order', 'order' => 'ASC', 'child_of' => 0, 'post_parent' => 0)); 
    if ($posts) { 
     echo '<ul class="custom-nav ' . esc_attr($post_type) . '-nav">'; 
     foreach ($posts as $p) { 
      $child_posts = get_posts(array('post_type' => $post_type, 'numberposts' => -1, 'orderby' => 'menu_order', 'order' => 'ASC', 'child_of' => $p->ID, 'post_parent' => $p->ID, 'fields' => 'ids')); 
      $child = $child_posts ? ' <span class="submenus-count">(' . count($child_posts) . ')</span>' : ''; 
      $class = ''; 
      if ($p->ID == $current) { 
       $class = 'current_page_item'; 
      } elseif ($current && in_array($current, $child_posts)) { 
       $class = 'current_page_ancestor'; 
      } 
      $class = '' != $class ? ' class="' . $class . '"' : ''; 
      echo '<li' . $class . '><a href="' . get_permalink($p->ID) . '">' . get_the_title($p->ID) . '</a>' . $child . '</li>'; 
     } 
     echo '</ul>'; 
    } 

} 

然後你可以顯示你的菜單:

<?php my_custom_nav('custom_post_type'); ?> 
+0

感謝這個工程。有沒有依靠wp菜單系統來做到這一點?我沒有子導航只需要顯示多少個子頁面。 –

+0

是的,你是否想列出所有的頂級頁面? –

+0

是的,我應該在前言中說它是來自自定義帖子類型的項目,它是分層的。 –