导航菜单和显示帖子计数

时间:2013-02-26 作者:jamal

Iam正在寻找显示子帖子数量的解决方案,如下所示:

父母(14)-子女-子女

父(2)-子

我玩过自定义walker,并尝试了->count的功能,但运气不好。

有人有解决方案吗?谢谢

class Count_Post_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\' => \'post\', \'show_count\' => 1, \'numberposts\' => -1,  \'meta_query\' => array(
       array(
            \'taxonomy\' => \'category\',
            \'field\' => $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 );
    }
}

1 个回复
SO网友:david.binda

是的,我找到了这个问题的解决办法。您可以从检索的menu\\u items中检索子项的数量wp_get_nav_menu_items. 此函数用于检索一组包含“menu\\u item\\u parent”信息的post对象。

在Walker\\u Nav\\u菜单类的start\\u el函数中,您可以在$item->ID中尝试匹配。方法如下:

$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations[\'primary\'] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
$count = 0;
foreach( $menu_items as $menu_item ){
    if( $menu_item->menu_item_parent == $item->ID ){
        $count++;           
    }       
}
然后在$count变量中得到子项的计数。以下是完整代码:

class Count\\u Post\\u Walker扩展Walker\\u Nav\\u菜单{

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;
    $classes[] = \'menu-item-\' . $item->ID;

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

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

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

    /* Code to get children count */
    $locations = get_nav_menu_locations();
    $menu = wp_get_nav_menu_object( $locations[\'primary\'] );
    $menu_items = wp_get_nav_menu_items($menu->term_id);
    $count = 0;
    foreach( $menu_items as $menu_item ){
        if( $menu_item->menu_item_parent == $item->ID ){
            $count++;           
        }       
    }
    $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>\';
    if( $count > 0 )
        $item_output .= \'(\'.$count.\')\';
    $item_output .= $args->after;

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

结束

相关推荐

WooCommerce dynamic menus

我有一个基本的WordPress网站,设置了Suffusion主题和WooCommerce插件。这在大多数情况下都很有效,但是当我让人们点击/悬停在“Shop”菜单图标上时,我希望它能够动态地下拉我定义的产品类别。我如何做到这一点?我肯定有关于这个主题的文档,但我不确定要查找什么。我主要是一名系统管理员。