2016-11-05 246 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 –

相關問題