2014-01-23 124 views
-1

我需要從類別的子類別中獲取最新的5篇文章。 我該怎麼做?從WordPress的循環中獲取類別子類別的最新5篇文章

//編輯 所以我得到它的工作,每子類別職位的固定ammount的:

<?php $descendants = get_categories(array('child_of' => 3)); ?> 
<?php $cnt=1; 
foreach ($descendants as $child) { ?> 
<?php $catPosts = new WP_Query(); 
    $catPosts -> query("showposts=3&cat=$child->term_id"); 
?> 
<ul class="postPreviews"> 
<?php while ($catPosts->have_posts()) : $catPosts->the_post(); ?> 
<li><a href="<?php the_permalink() ?>"> 
    <div class="descOverlay"> 
     <img src="<?php the_field('teaserimage'); ?>" /> 
     <div class="overlayTitle"> 
      <div class="imgwrap"><img src="<?php echo get_template_directory_uri(); ?>/opaq.png"></div> 
      <span><?php the_title(); ?></span></div> 
     </div> 
    </a></li> 
<?php endwhile; 
}?> 
+0

我覺得這個問題很清楚,即使它很短。如果您真的想幫助並需要更多信息,請告訴我。 – schliflo

+0

讓我擴大,所以問題應該包含代碼,應該告訴我們你試過什麼,它失敗的地方... – 2014-01-23 20:20:52

+0

我沒有嘗試任何東西,因爲我想不出一個解決方案。我試圖在Google上爲我的問題找到答案,但沒有成功。 – schliflo

回答

2

我將通過所有子類別使用get_categories()循環和其ID添加到一個數組。然後,您可以使用該數組作爲category_in'參數來創建新的WP_Query。

<?php 
    $categories = get_categories(array(
     'child_of'=>'your_category_id' 
    )); 

    $subcategories = array(); 

    foreach ($categories as $category) { 
     $subcategories[] = $category->cat_ID; 
    } 
?> 

<?php 
    $new_loop = new WP_Query(array(
    'post_type' => 'post', 
    'category__in' => $subcategories, 
    'posts_per_page' => 5 
    )); 
?> 

<?php if ($new_loop->have_posts()) : while ($new_loop->have_posts()) : $new_loop->the_post(); ?> 

    // put your inside the loop code here 

<?php endwhile; else: ?> 
    No posts found 
<?php endif; ?> 
<?php wp_reset_query(); ?> 
+0

正是我在找的東西。非常感謝你! – schliflo

相關問題