Php 仅显示一个子类别中的帖子-Wordpress

Php 仅显示一个子类别中的帖子-Wordpress,php,wordpress,Php,Wordpress,假设我有以下类别 Movies ----- Action ----- Science fiction ----- Drama Music ----- POP ----- Rock 在这里,电影和音乐是父类别,其余是子类别 Movies ----- Action ----- Science fiction ----- Drama Music ----- POP ----- Rock 如果我只想显示音乐类别中流行音乐的流行音乐帖子,我该怎么做呢 请帮忙 我不知道如何显示子类别的帖子。

假设我有以下类别

Movies

----- Action
----- Science fiction
----- Drama

Music

----- POP
----- Rock
在这里,电影和音乐是父类别,其余是子类别

Movies

----- Action
----- Science fiction
----- Drama

Music

----- POP
----- Rock
如果我只想显示音乐类别中流行音乐的流行音乐帖子,我该怎么做呢

请帮忙

我不知道如何显示子类别的帖子。我只有一个示例代码,可以显示父类和子类帖子

<?
// Get the post ID
        $id = get_the_ID();         


        //get all the categories ( this function will return all categories for a post in an array)
        $category= get_the_category( $id );

        if ($category->category_parent == 0) {

        //extract the first category from the array
        $catID = $category[0]->cat_ID;

        //Assign the category ID into the query
        $verticalNavigationSwitcher = "cat=$catID&orderby=ID&order=ASC";
     }              


        $result = new WP_Query($verticalNavigationSwitcher);


                    //$featuredPosts->query('showposts=5&cat=3');
                    while ($result->have_posts()) : $result->the_post(); 
        ?>


   <li><a href='<?php the_permalink() ?>'><span><?php the_title(); ?></span></a></li>


  <?php 
            endwhile; 
            wp_reset_postdata();
 ?> 

看起来您正在使用的示例过于复杂了。如果您只是需要来自特定类别的帖子,那么您可以使用类别slug或ID来检索帖子,并使用WP_查询,如上面的示例所示

因此,如果“Pop”类别的id为4,您可以通过将鼠标悬停在Posts->Categories中的类别名称上来获取类别id…它显示在编辑链接中,那么您的代码将为

<?php
$result = new WP_Query( 'cat=4' );

while ($result->have_posts()) : $result->the_post(); 
?>
以下是法典的相关章节


我找到了一种只显示一个子类帖子的方法

$verticalNavigationSwitcher = "category__in=$catID&orderby=ID&order=ASC";
如果我在查询中使用category__,它将返回一个类别的帖子

$verticalNavigationSwitcher = "category__in=$catID&orderby=ID&order=ASC";

举一个例子,你通常如何显示结果,对于那些不熟悉WordPress的人,显示你尝试过的代码。嗨,Royal和Rikesh,我添加了一个示例代码。目前我不知道如何显示子类别中的帖子。此代码将显示类别及其子类别中的所有帖子。