query_postsでカテゴリ指定する際に子カテゴリーも含めた結果を取得する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$args = array( | |
'posts_per_page' => 10, //取得する結果数 | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'category', | |
'terms' => array(111, 222, 333), //カテゴリーIDを指定する | |
'include_children' => true, | |
'field' => 'term_id', | |
'operator' => 'IN' | |
), | |
'relation' => 'AND' | |
) | |
); | |
query_posts( $args ); //クエリの作成 | |
if ( have_posts() ) : while ( have_posts() ) : the_post(); | |
echo '<p>コンテンツの内容を書く</p>'; | |
endwhile; | |
else : | |
echo '<p>Not Found</p>';//見つからない時のメッセージ | |
endif; | |
wp_reset_query(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment