简体   繁体   English

wordpress仅在子类别上显示帖子

[英]wordpress display posts on subcategory only

hi I'm using Wordpress on developing a site and a self made template. 嗨,我正在使用Wordpress开发网站和自制模板。 I got 1 page to display all categories with its post. 我有1页显示其帖子的所有类别。 this is what i want to do 这就是我想做的

  • Main Category 主分类
    • subcategory1 子类别1
      • post 1 发布1
      • subcategory1.2 子类别1.2
        • post 1.2.1 发布1.2.1
        • post 1.2.2 发布1.2.2
    • subcategory2 子类别2
    • subcategory3 子类别3

i use wp_link_category() to display the category and i search how to display category with post but the problem is. 我使用wp_link_category()显示类别,我搜索如何显示带有帖子的类别,但问题是。 the post from subcategory 1.2 is also display on subcategory 1 子类别1中的帖子也显示在子类别1中

  • Main Category 主分类
    • subcategory1 子类别1
      • post 1 发布1
      • post 1.2.1 发布1.2.1
      • post 1.2.2 发布1.2.2
      • subcategory1.2 子类别1.2
        • post 1.2.1 发布1.2.1
        • post 1.2.2 发布1.2.2
    • subcategory2 子类别2
    • subcategory3 子类别3

how can i remove the post from subcategory 1.2 on subcategory 1? 如何从子类别1的子类别1.2中删除帖子? here's the code i copy and paste from stackoverflow 这是我从stackoverflow复制并粘贴的代码

$categories =  get_categories('child_of=4');  
foreach  ($categories as $category) {
    //Display the sub category information using $category values like $category->cat_name
    echo '<h2>'.$category->name.'</h2>';
    echo '<ul>';

    foreach (get_posts('cat='.$category->term_id) as $post) {
        setup_postdata( $post );
        echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></li>';   
    }  
    echo '</ul>';
}

i hope you understand my bad english. 我希望你能听懂我的英语不好。

try this instead: 试试这个代替:

$args = array(
  'child_of' => 4,
  'parent' => 4
  );

$categories =  get_categories( $args ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM