使用自定义柱子类型扩展Walker类

时间:2012-11-03 作者:acy

我正在使用下面的walker类,但我无法将其用于自定义帖子。

 class Zwp_Walker_Nav_Menu_Dynamic extends Walker_Nav_Menu {
    function end_el(&$output, $item, $depth) {
        global $wp_query;
        global $post;
        $thePostID = $post->ID;

        $subposts = get_posts( \'numberposts=10&cat=\' . $item->object_id );

        if ( $subposts ) {

            $output .= \'        <ul class="\';

            if ( $item->object_id == $post->ID && 100 == 101) { $output .= \' sub-menu-current\'; } else { $output .= \' sub-menu\'; }
            $output .=  \' sub-menu-item-\'.$item->object_id.\'">
            \';
            foreach( $subposts as $post ) :
                setup_postdata( $post );
                $output .= \'<li\';
                if ( is_single() && $wp_query->post->ID == $post->ID ) { 
                    $output .= \' class="current"\'; 
                }
                $output .= \'><a href="\'. get_permalink( $post->ID ) .\'">\' . get_the_title( $post->ID ) . \'</a></li>
                \';
            endforeach;
            $output .= \'</ul>
            \';

        }

    }
}   
我试图更改get\\u posts查询,但得到了奇怪的结果。(可能是因为缺少ID)

$subposts = get_posts( \'numberposts=10&post_type=portfolio_work\' );
结果:

Design »
    Project 1
    Project 2
Photography »
    Project 1
    Project 2
About »
    Project 1
    Project 2
它应该是这样的:

Design »
    Project 1
Photography »
    Project 2
About

1 个回复
SO网友:Vincent Mimoun-Prat

修复100 == 101 在你身上if 语句(始终为false)

  • 尝试$subposts = get_posts( \'numberposts=10&post_type=portfolio_work&cat=\' . $item->object_id );
  • 结束

    相关推荐

    Custom menus not showing

    作为我上一次关于菜单的未解决查询的后续,这个问题已经进一步扩展。我的菜单没有打印代码中的任何地方。我正在注册菜单功能。php:add_action( \'after_setup_theme\', \'your_newtheme_setup\' ); if ( ! function_exists( \'your_newtheme_setup\' ) ) : function your_newtheme_setup() { if (