実現したいこと
WordPressの記事一覧ページで各記事が属する親カテゴリのみを表示したいです。
また、カテゴリ名をクリックすると該当のカテゴリに属した記事のみの一覧ページを表示したいです。
前提
運用中のWordPressに少し手を加えたいと考えています。
記事一覧、記事詳細、カテゴリページ(category.php)は作成済みです。
記事一覧ページ(<div id="wpLoop">)、記事詳細ページ(<div id="wpSingle">)をindex.phpに記述しています。
1記事に親カテゴリ、子カテゴリを含む複数のカテゴリを設定しています。
上記を実現するためのソースコードを教えていただけないでしょうか。
下記ソース内に追記したいと考えています。
<div id="wpLoop"> <!--#mainimg--> <?php $paged = get_query_var( 'paged' ) ? : 1; $args = array( 'paged' => $paged, 'posts_per_page' => 20, ); $the_query = new WP_Query( $args ); $recodes = []; if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); $previousday = ''; $date = get_the_date( $the_query->ID ); $title = get_the_title( $the_query->ID ); $url = get_permalink( $the_query->ID ); $cats = get_the_category( $the_query->ID ); $cat = $cats[ 0 ]; $author = get_the_author( $the_query->ID ); $excerpt = get_the_excerpt( $the_query->ID ); if ( has_post_thumbnail() ) { $thumbnail = get_the_post_thumbnail_url( $the_query->ID, 'full' ); } else { $thumbnail = get_theme_file_uri() . "/img/no_img.png"; } ?> <div class="item <?php echo implode(" ", get_post_class()); //記事クラス ?> blog_bg"> <div class="thumb"> <a href="<?=$url?>"> <img src="<?=$thumbnail?>" width="100%"> </a> </div> <div class="rightBox"> <h2 class="blog_tit"><a href="<?=$url?>"> <?=$title?> </a></h2> <div class="meta"> <span class="post_date"> <?=$date?> </span> <span class="post_author">書いた人: <?=$author?> </span> </div> <div> </div> <div class="body"> <?=$excerpt?> <div class="btn_continue"> <a href="<?=$url?>">続きを読む</a> </div> </div> </div> </div> <!--.item--> <?php } //while } //if ?> </div>
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。