2017-04-25 3 views
1

親を持つ各カテゴリ(子カテゴリ)から最新の投稿を出力したいと思います。親カテゴリIDは54です。特定の親カテゴリの各子カテゴリの最新投稿を出力

たとえば、カテゴリ54の下に7つの子カテゴリがある場合、出力ポストの数は7(すべての子カテゴリから最新)です。私はこれが理にかなってほしい。

私の現在のコードは以下の通りです。この段階では、このコードは、cat id = 54の最新のものを持つ最新の投稿(1つの子カテゴリの1つ)のみを出力します。複数の子カテゴリから最新の投稿を取得できるように、これを修正する方法を教えてもらえれば幸いです。ここで

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

ありがとうございました。これは私の元のコードと同じように動作します。単一の子カテゴリから最新の投稿を1つだけ出力します。 1つ以上(現在7つ)のカテゴリがあり、それらのカテゴリに最新の投稿があります。出力ポストの数は7になるはずです。 – Palmtree

+0

スレッドはここに続きます https://wordpress.org/support/topic/output-latest-posts-from-each-child-categories-of-particular-親カテゴリ/#post-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. 
} 

がtax_queryに$のterm_idを渡し、これを試してみてください、あなたが使用する必要があるロジックです。

$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」になると、1つの子カテゴリからの最新の投稿が1つだけ正しいことを意味しますか? 上記の私の元のコードでも、posts_per_pageの値を変更することで複数を出力することができます。 – Palmtree

関連する問題