douzheng5717 2013-03-10 16:29
浏览 40

Wordpress显示补充工具栏中帖子子类别下的所有子类别+帖子

Sorry for the complicated title.

I am working with this code, to display my sub child categorys with posts under them. The problem is that the code is displaying ALL Sub child categorys (+ posts) that I have on the site. I only want to show the Sub categorys that have connection with the post. All Sub child categorys under Child one have connection with the posts, so you can say I want to display the sub child categorys under child on becuse the post is related with child on.

Category structure (Year is in the title):

  • G
    • Child 1
      • Gaming Year 1
        • Post for here
      • Gaming Year 2
        • Post for here

My code:

<?php
$cat_id = get_query_var( 'cat' );
$subcats = get_categories( 'child_of=' . $cat_id ); // child categories

class Cat_Walker extends Walker_Category {
    function end_el( &$output, $page, $depth = 0, $args = array() ) {
        $posts = get_posts( 'cat=' . $page->term_id );

        if ( sizeof( $posts ) > 0 ) {
            $output .= '<ul>';

            foreach ( $posts as $post ) {
                $output .= sprintf( '<li><a href="%1$s">%2$s</a></li>', get_permalink( $post->ID ), $post->post_title );
            }

            $output .= '</ul>';
        }

        $output .= '</li>';
    }
}

foreach ( $subcats as $subcat ) {
    $subsubcats = get_categories( 'child_of=' . $subcat->term_id ); // sub child categories

    foreach ( $subsubcats as $subsubcat ) {
        $args = array(
            'title_li'         => '',
            'show_option_none' => '',
            'taxonomy'         => 'category',
            'child_of'         => $subsubcat->term_id,
            'walker'           => new Cat_Walker( )
        );

        wp_list_categories( $args );
    }
}

?>

Any ideas?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongtang6775 2013-03-11 06:46
    关注

    I believe you are getting the wrong Category ID. You should use this:

    $category = get_category(get_query_var('cat'));
    $cat_id = $category->cat_ID;
    

    Instead of

    $cat_id = get_query_var( 'cat' );
    
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来