Last active
October 18, 2017 05:39
-
-
Save mypacecreator/74479945618bb436cb3870ebcbc329c9 to your computer and use it in GitHub Desktop.
get_termsとget_postsの入れ子例 改(複数タームに属する記事は2回登場しないように)
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 | |
$displayed_post_id = array(); | |
$terms = get_terms( | |
'faqcategory', array( | |
'orderby' => 'slug', | |
'order' => 'ASC' | |
) | |
); | |
foreach( $terms as $term ): | |
?> | |
<?php | |
$cat_posts = get_posts( array ( | |
'taxonomy' => 'faqcategory', | |
'term' => $term->slug, | |
//'category_name' => $term->slug, /* カテゴリーの場合 */ | |
'post__not_in' => $displayed_post_id | |
) | |
); | |
if( $cat_posts ): | |
?> | |
<h2><?php echo esc_html( $term->name ); ?></h2> | |
<ul> | |
<?php | |
foreach( $cat_posts as $post ): | |
setup_postdata( $post ); | |
?> | |
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> | |
<?php | |
$displayed_post_id[] = $post->ID; | |
endforeach; wp_reset_postdata(); | |
?> | |
</ul> | |
<?php endif; ?> | |
<?php endforeach; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment