2017-04-25 85 views
1

我想從每個具有父類別的類別(子類別)輸出最新帖子。父類別ID爲54.輸出來自特定父類別的每個子類別的最新帖子

例如,如果類別54下有7個子類別,則輸出帖子的數量應該是7(每個子類別都是最新的)。我希望這是有道理的。

我目前的代碼如下。在此階段,此代碼僅輸出一個最新的帖子(1個子類別),該帖子具有最新的cat id = 54。如果你能告訴我如何修改這個,以便我可以從多個子類別獲得更多最新帖子,那將是非常好的。

<?php 
$categories = get_categories(); 
foreach ($categories as $category) { 
    $args = array(
    'cat' => 54, 
    'post_type' => 'post', 
    'posts_per_page' => '1', 
    ); 
} 
?> 
<?php $query = new WP_Query($args); ?> 
<?php if ($query->have_posts()) : ?> 
<div class="container"> 

<?php while ($query->have_posts()) : $query->the_post(); ?> 
<div class="box"> 
<article> 
    <p><?php foreach((get_the_category()) as $childcat) { if (cat_is_ancestor_of(54, $childcat)) { echo '<a href="'.get_category_link($childcat->cat_ID).'">'; echo $childcat->cat_name . '</a>'; }} ?></p> 
    <?php if (has_post_thumbnail()): ?><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('box-pic'); ?></a><?php endif; ?> 
    <h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3> 
</article> 
</div> 
<?php endwhile;?> 

</div> 
<?php endif; ?> 
<?php wp_reset_query(); ?> 

回答

0

以下是你需要使用的邏輯,

$term_id = 54; 
$taxonomy_name = 'category'; 
$term_children = get_term_children($term_id, $taxonomy_name); 

echo '<ul>'; 
foreach ($term_children as $child) { 
    $term = get_term_by('id', $child, $taxonomy_name); 

    $args = array(
    'cat' => $term->term_id, 
    'post_type' => 'post', 
    'posts_per_page' => '1', 
    ); 
    $query = new WP_Query($args); ?> 
    <?php if ($query->have_posts()) : ?> 
    <div class="container"> 

    <?php while ($query->have_posts()) : $query->the_post(); ?> 
    <div class="box"> 
    <article> 
    <p><?php foreach((get_the_category()) as $childcat) { if (cat_is_ancestor_of(54, $childcat)) { echo '<a href="'.get_category_link($childcat->cat_ID).'">'; echo $childcat->cat_name . '</a>'; }} ?></p> 
    <?php if (has_post_thumbnail()): ?><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('box-pic'); ?></a><?php endif; ?> 
    <h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a> 
    </h3> 
    </article> 
    </div> 
    <?php endwhile;?> 

    </div> 
    <?php endif; ?> 
     <?php wp_reset_query(); 
} 
echo '</ul>'; 
+0

謝謝Ashmed。這和我原來的代碼一樣。它只輸出來自單個孩子類別的最新帖子。有超過1(目前7)類別,這些類別有他們的最新帖子。這意味着輸出帖子的數量應該是7. – Palmtree

+0

線程在此繼續.. https://wordpress.org/support/topic/output-latest-posts-from-each-child-categories-of-particular-父類別/#後9072769 – Palmtree

0

試試這個,

$term_id =54 ; 
$term_children = get_term_children($term_id, $taxonomy); 
$term_id = array(); 

foreach ($term_children as $child) { 
    $term = get_term_by('id', $child, $taxonomy); 
    $term_id[] = $term->term_id; //childern ids array. 
} 

傳遞$ term_id到tax_query。

$args = array(
     'post_type' => $post_type, 
     'posts_per_page' => 7, 
     'tax_query' => array(
      array(
       'taxonomy' => $taxonomy, 
       'field' => 'term_id', 
       'terms' => $term_id 
      ) 
     ) 
    ); 
    $query = new WP_Query($args); 

這會爲你工作。

+0

感謝您的這一點,但你的代碼沒有輸出任何東西。此外,可以更改子類別的數量。現在是7,但將來會或多或少。因此,我必須避免像7. 以防萬一,如果沒有誤解,「posts_per_page」爲「1」意味着只有一個最新的單個子類別的職位是正確的嗎? 即使我上面的原始代碼可以通過更改posts_per_page值來輸出多個。 – Palmtree

相關問題