Skip to content

Instantly share code, notes, and snippets.

@mypacecreator
Last active October 18, 2017 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mypacecreator/74479945618bb436cb3870ebcbc329c9 to your computer and use it in GitHub Desktop.
Save mypacecreator/74479945618bb436cb3870ebcbc329c9 to your computer and use it in GitHub Desktop.
get_termsとget_postsの入れ子例 改(複数タームに属する記事は2回登場しないように)
<?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