php - Duplicate Term with get_the_terms() -
i'm having difficulty listing custom post type unique category/taxonomy headings. have acf reverse relationship , have 2 articles under category 1 , 1 article under category 2. next, i'm attempting loop through each category , list them out so:
category 1
- article
- article
category 2
- article
however, below returning is:
category 1
- article
category 1
- article
category 2
article
$research = get_posts(array( 'post_type' => 'research-data', 'meta_query' => array( array( 'key' => 'show_on_page', 'value' => '"' . get_the_id() . '"', // matches exaclty "123", not 123. 'compare' => 'like' ) ) )); ?> <?php if( $research ): ?> <h3> research & data</h3> <?php foreach( $research $r ): ?> <!-- begin custom tax loop --> <?php $categories = get_the_terms($r->id, 'research-cats', $term_args); $c_terms = array(); foreach ( $categories $term ) { $c_terms[] = $term->name; } $unique_cat = array_unique($c_terms); //print_r($unique_cat); ?> <strong><?php echo $unique_cat[0]; ?></strong> <ul> <?php $posts = get_posts(array( 'post_type' => 'research-data', 'orderby' => 'menu_order', 'order' => 'asc', 'post__in' => array($r->id), 'nopaging' => true, )); foreach($posts $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a></li> </ul> <?php endforeach; ?> <?php endforeach; ?> <?php endif; ?>
any thoughts? driving me nuts!
$research = get_posts(array( 'post_type' => 'research-data', 'meta_query' => array( array( 'key' => 'show_on_page', 'value' => '"' . get_the_id() . '"', // matches exaclty "123", not 123. 'compare' => 'like' ) ) )); $previous_category = ''; ?> <?php if( $research ): ?> <h3> research & data</h3> <?php foreach( $research $r ): ?> <!-- begin custom tax loop --> <?php $categories = get_the_terms($r->id, 'research-cats', $term_args); $c_terms = array(); foreach ( $categories $term ) { $c_terms[] = $term->name; } $unique_cat = array_unique($c_terms); //print_r($unique_cat); ?> <?php if($previous_category !== $unique_cat[0]) { ?><strong><?php $previous_category = $unique_cat[0]; echo $unique_cat[0]; ?></strong><?php } ?> <ul> <?php $posts = get_posts(array( 'post_type' => 'research-data', 'orderby' => 'menu_order', 'order' => 'asc', 'post__in' => array($r->id), 'nopaging' => true, )); foreach($posts $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a></li> </ul> <?php endforeach; ?> <?php endforeach; ?> <?php endif; ?>
without knowing how getting data , format of $research hard guessing if use above addition of $previous_category variable should prevent behaviour
Comments
Post a Comment