During the foreach loop, if the same term is found, add a link to the page, otherwise add a name for the name and a link to the page during one wp_query?
So for example:
- Header 1 Apple Pie
- Heading 1 Cherry Pie
- Header 2 Ham Pie
- Header 3 Goat Pie
- Heading 1 Noodle Cake
Must become:
- Heading 1
- Apple Pie
- Cherry pie
- Noodle cake
<?php $alternate_edition_a = array( 'post_type'=> 'alternate_edition', 'post_status' => 'publish', 'posts_per_page'=>10, 'no_found_rows' => true, 'tax_query' => array( array( 'taxonomy' => 'game_titles', 'field' => 'slug', 'terms' => $gametitle_con, ) ) ); $alternate_edition_query = new WP_Query( $alternate_edition_a ); if($alternate_edition_query->have_posts() ) : while ( $alternate_edition_query->have_posts() ) : $alternate_edition_query->the_post(); $alternate_editionTitlesID[] = get_the_ID(); $systemformats_altedition_terms = get_the_terms($post->id, 'system_formats'); foreach ($systemformats_altedition_terms as $systemformats_altedition_term) { if (!has_term($systemformats_altedition_term->slug, 'system_formats', $gameFeatTitleID)) { $systemterm_slug[] = $systemformats_altedition_term->slug; } } endwhile; else: endif; wp_reset_postdata(); $array_notpresentclean = array_unique($systemterm_slug); foreach ($array_notpresentclean as $array_notpresentclean2) { $alternate_edition_a2 = array( 'post_type'=> 'alternate_edition', 'post_status' => 'publish', 'posts_per_page'=>10, 'no_found_rows' => true, 'post__in' => $alternate_editionTitlesID, 'tax_query' => array( array( 'taxonomy' => 'system_formats', 'field' => 'slug', 'terms' => $array_notpresentclean2, ), ), ); $alternate_edition_query2 = new WP_Query( $alternate_edition_a2 ); if($alternate_edition_query2->have_posts() ) : while ( $alternate_edition_query2->have_posts() ) : $alternate_edition_query2->the_post(); $currentterm_obj = get_term_by('slug', $array_notpresentclean2, 'system_formats'); if (!isset($var_notpresent)) { $var_notpresent = 1; ?> <a href="<?php echo esc_url( home_url( '/' ) ); ?>system_information/<?php echo $currentterm_obj->slug; ?>" title="<?php echo $currentterm_obj->description;?>"><?php echo $currentterm_obj->name;?></a> <?php } ?> <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php echo get_the_title(); ?>"><?php echo get_the_title(); ?></a></li> <?php endwhile; else: endif; wp_reset_postdata(); unset($var_notpresent); } ?>
Output:
- Cheese
- Steam
- Cake is LIE
- Bennasa Saw Banananaa
- Gears of War: Ultimate Edition Windows 10
- Sega Saturn
- Nintendo
- Bennasa Saw Banananaa
- Gears of War: Ultimate Edition
- Xbox one
- Bennasa Saw Banananaa
- Hamsters WHAR
- Gears of War: Ultimate Edition Windows 10
- Gears of War: Ultimate Edition
source share