2016-11-05 249 views
2

考虑:在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 - > ChildPage。我需要显示的子页面...

+0

方的评论。你不需要在每一行中使用打开和关闭php标签。你可以使'while(have_posts()):the_post(); ENDWHILE; else:'... – nanocv

+0

谢谢..但如何从上述查询中获取子类别? –

+0

你的意思是“显示来自子类别的帖子”? – Theunis

回答

0

您必须查询该父类的孩子,通过get_categories

$child_categories = get_categories(array(
    'orderby' => 'name', 
    'parent' => 0 
)); 

与父类的ID替换0,现在使用的阵列子类来获得数据, 使用WP_Query,而不是˚Fquery_posts

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

非常感谢你..它真的帮助了我! :) –

+0

你可以请upvote答案,并添加为最好的? @BikashGurung –

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 
    } 
?> 

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

+0

请问您如何从父类中获取子类?@BikashGurung –

相关问题