单亲类别的 Wordpress 输出类别子帖子

Wordpress output Category Children Posts of single Parent Category

对我目前拥有的东西有点挣扎。所以我想遍历一个父类别并输出所有带有 link.

的子类别帖子

我的当前代码如下,它为每个父类别和子类别创建一个列表,因此:

我觉得我的 $args 可以解决这个问题。

<?php  

    $get_queried_object = get_queried_object();

    $cats = get_categories( array( 
        'child_of'   => $get_queried_object->term_id,
        'hide_empty' => false
    ) ); 

    foreach ($cats as $cat) :

        $args = array(
                    'category_name' => 'services', // Use the category id, can also replace with category_name which uses category slug
          'cat' => $cat->term_id
        );

        $my_query = new WP_Query($args); ?>

            

        <?php 
        if ($my_query->have_posts()) :
        echo '<h5>'.$cat->name.'</h5>'; ?>


            <ul>
                <?php while ($my_query->have_posts()) : $my_query->the_post();  ?>
                    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
                <?php endwhile; wp_reset_postdata(); ?>
            </ul>
        <?php else : 

            echo 'No Posts for '.$cat->name;                

        endif; 

    endforeach; 
?>

所以这显示了下面,我已经把我们想要的放在右边,还显示了类别设置。

使用 cat 而不是 category__in 并且您还需要 运行 wp_reset_postdata() 之后再次使用主查询的当前 post。试试下面的代码。

<?php  

    $get_queried_object = get_queried_object();

    $cats = get_categories( array( 
        'child_of'   => $get_queried_object->term_id,
        'hide_empty' => false
    ) ); 

    foreach ($cats as $cat) :

        $args = array(
          'cat' => $cat->term_id
        );

        $my_query = new WP_Query($args); 

        if ($my_query->have_posts()) : ?>
            <ul>
                <?php while ($my_query->have_posts()) : $my_query->the_post();  ?>
                    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
                <?php endwhile; wp_reset_postdata(); ?>
            </ul>
        <?php else : 

            echo 'No Posts for '.$cat->name;                

        endif; 

    endforeach; 
?>

根据 OP 代码更新。

<?php
/**
 * Template Name: Home
 * Description: Page template with custom content
 *
 */

get_header();

?>

<section>
    <div class="container">
        <div class="row">
            <div class="col">
                <?php  
    
                    $cats = get_categories( array( 
                        'child_of'   => 2,
                        'hide_empty' => false
                    ) ); 

                    foreach ($cats as $cat) :

                        $args = array(
                          'category_name' => 'services', // Use the category id, can also replace with category_name which uses category slug
                          'cat' => $cat->term_id
                        );

                        $my_query = new WP_Query($args); ?>

                        <?php 
                        if ($my_query->have_posts()) :
                            echo '<h5>'.$cat->name.'</h5>'; ?>
                            <ul>
                                <?php while ($my_query->have_posts()) : $my_query->the_post();  ?>
                                    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
                                <?php endwhile; wp_reset_postdata(); ?>
                            </ul>
                        <?php else : 

                            echo 'No Posts for '.$cat->name."</br>";

                        endif; 

                    endforeach; 
                ?>
            </div>
        </div>
    </div>
</section>

<?php
get_footer();
?>