
【wordpress】カスタムフィールドの値を取得しquery_postsに指定
Date 2017.03.18 Category blog
wordpressでカスタムフィールドで値を指定しquery_postsなどに指定する
固定ページにカスタムフィールドを設定し任意のカスタム投稿の記事を表示した。
通常であればquery_postsにカスタム投稿スラッグを指定すれば良いが固定ページ作成時に任意のカスタム投稿を指定して
ページを作成できるテンプレートを構築するときのメモ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<!-- 'key'カスタムフィールド値を$hogeに格納 --> <?php $hoge= get_post_meta($post->ID,'key',true); ?> <!-- ここからquery_postsで取得したい記事をしてい --> <?php $args=array( 'tax_query' => array( array( 'taxonomy' => 'after', //タクソノミーを指定 'field' => 'slug', //ターム名をスラッグで指定する 'operator' => 'AND', 'terms' => array('terms01','terms02') //表示したいタームをスラッグで指定複数の場合 ), ), 'post_type' => $hoge, //最初に指定したカスタムフィールド値をここで代入 'posts_per_page'=> -1 //表示件数(-1で全ての記事を表示) ); ?> <?php query_posts( $args ); ?> <?php if(have_posts()): ?> <?php while(have_posts()):the_post(); ?> <div> <h4><?php the_title(); ?></h4> <?php the_content();?> </div> <?php endwhile; endif; ?> <?php wp_reset_query(); ?> |