サポート » その他 » singleページで特定のカテゴリを除外する

  • 解決済 emi1106

    (@emi1106)


    singleページをスラッグ「news」と「blog」をsingle-news.phpとsingle-blog.phpに分けていて、
    single-blog.phpでの親カテゴリ「blog」の子カテゴリ一覧を表示しています。

    「blog」の子カテゴリのみを表示したいのに、「news」の記事も表示されてしまい、困っています。

    —–

    <dl>
    <?php
    global $post;
    $cats = get_the_category(); 
    
    $cat_posts = get_posts(array(
    	'post_type' => 'post', // 投稿タイプ
    	'posts_per_page' => 5, // 表示件数
    	'orderby' => 'date', // 表示順の基準
    	'order' => 'DESC', // 昇順・降順
    ));
    $count = count($cat_posts);
    
    if ($count >= 1): foreach ($cat_posts as $post): setup_postdata($post);?>
    
    										<dt><?php the_time('Y.m.d');?><span><?php
        $cats = get_the_category();
        foreach($cats as $cat):
        if($cat->parent) echo $cat->cat_name;
        endforeach;
    ?></span></dt><dd id="post-<?php the_ID();?>"><a href="<?php the_permalink()?>"><?php the_title();?></a></dd>
    										<?php endforeach;
        wp_reset_postdata();else: ?>
    			<p>記事がありません。</p>
    
    <?php endif;?>
    
    </dl>

    —–

    色々調べましたが上手くいかず、どのように修正すれば「news」の記事が出ないように出来るか教えていだだきたいです。

    • このトピックはemi1106が1年、 7ヶ月前に変更しました。
    • このトピックはemi1106が1年、 7ヶ月前に変更しました。

    ヘルプの必要なページ: [リンクを見るにはログイン]

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック投稿者 emi1106

    (@emi1106)

    下記で解決しました。

    <dl>
    <?php
    global $post;
    $args = array( 'posts_per_page' => 5, 'category' => 10 );
    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) : 
      setup_postdata( $post ); ?>
    										<dt><?php the_time('Y.m.d');?><span><?php
        $cats = get_the_category();
        foreach($cats as $cate):
        if($cate->parent) echo $cate->cat_name;
        endforeach;
    ?></span></dt><dd id="post-<?php the_ID();?>"><a href="<?php the_permalink()?>"><?php the_title();?></a></dd>
    <?php endforeach; 
    wp_reset_postdata(); ?>
    </dl>
1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「singleページで特定のカテゴリを除外する」には新たに返信することはできません。