投稿タイプによって表示項目を変更したい
Wordpressの自作のテーマを改良しているのですが、
記事一覧に異なるpost_typeの投稿投稿日時順に表示をして、
さらにpost_type毎に違う要素を表示したいと考えております。
しかし、違うpost_typeの記事を表示することは出来たのですが、
そこからpost_type毎に条件分岐をすることが出来ません。
不勉強で申し申し訳ございませんが、
お力を貸していただけると幸いです。
現在のソースコード
php
1<?php 2$args = array( 3 'post_type' => array('news','sample'), 4); 5$wp_query = new WP_Query( $args ); ?> 6<?php 7if ( $wp_query->have_posts() ) { 8 while ( $wp_query->have_posts() ){ 9 $wp_query->the_post(); 10 ?> 11 <article> 12 <h2> 13 <?php the_title(); ?> 14 </h2> 15 <p> 16 <?php the_content(); ?> 17 </p> 18 </article> 19 <?php 20 } 21} 22?>
実装希望例
news
1 2 <h2> 3 newsタイトル 4 </h2> 5 <p> 6 記事の内容 7 </p> 8
sample
1 2 <h2 class="hoge">//post_typeによって表示を変えたい 3 sampleタイトル 4 </h2> 5 <p> 6 //定型文を表示 7 </p> 8
皆様のおかげでとりあえず動くようにはなりました。
今回はposttypeが2つしかなかったので以下でも問題なさそうです。
回答反映例
php
1 2 <?php 3$args = array( 4 'post_type' => array('post','sample'), 5); 6$wp_query = new WP_Query( $args ); ?> 7<?php 8if ( $wp_query->have_posts() ) { 9 while ( $wp_query->have_posts() ){ 10 $wp_query->the_post(); 11 ?> 12 <?php $posttype = get_post_type( get_the_ID() ); 13if ( $posttype == 'post' ) { 14 echo '<h2>' 15 . get_the_title() . 16 '</h2>'; 17} else { 18 echo '<h2 class="hoge">' . get_the_title() . '</h2><p> //定型文を表示 </p>'; 19} ?> 20 <?php 21 } 22} 23?> 24

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/15 12:35