首页 > 解决方案 > 适用于所有子类别的 WP 一个模板

问题描述

我有一个父类别“报告”和三个子类别“每日”、“每周”和“每月”。标识如下:

我想创建一个查询以在单个模板上使用,该模板仅显示来自相同子类别的帖子,并在创建新子类别时工作。现在我已经为每个子类别创建了一个页面并使用相同的模板。但是,我只能让它显示来自父级的帖子,因此它列出了每个子类别的帖子。

<?php
    $args = array(
        'post_status'           => 'publish',
        'post_type'             => 'post',
        'cat'                   => 4,
        // 'category__in' => array(8,9,10,11),
        'nopaging'              => false,
        'posts_per_page'        => '10',
        'ignore_sticky_posts'   => 0,
        'order'                 => 'DESC',
        'orderby'               => 'date',
    );

    // The Query
    $query = new WP_Query( $args );
    // The Loop
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post(); ?>
            <div class="excerpt">
                <h2 class="f-bold f-size28 f-l-height32 c-red">
                    <a class="f-bold f-size28 f-l-height32 c-red" href="<?php the_permalink(); ?>" title="View Full Post">
                    <?php the_title(); ?>
                    </a>
                </h2>
            </div>
            <? }
        } else {
            echo('no posts at this time');
        }

        wp_reset_postdata();
    ?>

如何从页面动态获取子类别的 ID 并仅显示基于该 ID 的帖子?

标签: phpwordpress

解决方案


推荐阅读