在WordPress中显示子类别的帖子?

时间:2016-11-05 11:39:40

标签: php wordpress

考虑:

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts( array('category_name' => 'parent','posts_per_page'=>'3','paged' => $paged));
    if(have_posts()):
        while(have_posts()):
?>

    <?php the_post(); ?>
    <?php endwhile; ?>
<?php else: ?>
    <div class="entry"> Sorry, no posts found. Please try the
        <a href="<?php bloginfo('home'); ?>">Homepage &rarr;</a>
    </div>
<?php endif; ?>

此代码仅显示页面的父帖子,但我想显示父类别下的页面:例如:ParentPage - &gt; ChildPage。我需要显示子页面...

2 个答案:

答案 0 :(得分:0)

您必须按get_categories

查询该父类别的子项
$child_categories = get_categories( array(
    'orderby' => 'name',
    'parent'  => 0
) );

将0替换为父类别的id,现在使用子类别数组来获取数据, 使用WP_Query代替f query_posts

$query = new WP_Query( array( 'cat' => '2,6,17,38' ) );

答案 1 :(得分:0)

<?php   
    $args = array( 'category_name' => 'child_category' );

    $the_query = new WP_Query( $args );

// The Loop
    if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
    $the_query->the_post();

    the_content();
    }
/* Restore original Post Data */
    wp_reset_postdata();
    } else {
 // no posts found
    }
?>

谢谢......这个解决了我的问题。 :)