合并相同标题下的条款,除非new =创建新的标题Wordpress查询

时间:2015-08-10 14:48:38

标签: php arrays wordpress foreach wp-query

在一个' foreach'循环,如果检测到相同的术语,只添加页面链接,否则在单个wp_query期间添加术语名称和页面链接?

例如:

  1. 标题1        Apple Pie
  2. 标题1        樱桃派
  3. 标题2        火腿派
  4. 标题3        山羊派
  5. 标题1        面条蛋糕
  6. 应该成为:

      • 标题1
        • Apple Pie,
        • Cherry Pie,
        • Noodle Cake
      • 标题2
        • Ham Pie
      • 标题3
        • Goat Pie
    1. <?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(); /*Important to reset away from alternate post type*/ 
      
      $array_notpresentclean = array_unique($systemterm_slug);
      
      /*print_r ($array_notpresentclean);
      */
      
      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(); /*Important to reset away from alternate post type*/ 
      
      unset($var_notpresent);
      
      } 
      
      
      
      ?>
      

      输出是:

      1. 干酪
        • 蛋糕是LIE
      2. 蒸汽
        • 蛋糕是LIE
        • Banananaa Chainsaw
        • 战争机器:终极版Windows 10
      3. 世嘉土星
        • 害怕钻孔
      4. 任天堂
        • Banananaa Chainsaw
        • 战争机器:终极版
      5. Xbox One
        • Banananaa Chainsaw
        • WHAR的仓鼠
        • 战争机器:终极版Windows 10
        • 战争机器:终极版

1 个答案:

答案 0 :(得分:1)

我希望这会有所帮助。

$titles = array('Title 1 Apple Pie','Title 1 Cherry Pie','Title 2 Ham Pie','Title 3 Goat Pie');
$list = '<ol>';
foreach ($titles as $value) {
$subtitle = substr($value, 0, 7);
if(!strpos($final,$subtitle )){
   $list .= '<li>' . $value . '</li>' ;
}  else {
    $pos = stripos($list, $subtitle);
    $left_string = substr($list,0, $pos);
    $substring = substr($list, $pos);
    $next_pos = stripos($substring, '<');
    $right_string = substr($substring, $next_pos);
    $current = substr($substring, 8, $next_pos);
    $current = rtrim($current, '</li>');
    $list = '';
    $last = $current . ' '. substr($value, 8);
    $list = $left_string .' '. $subtitle.' '. $last . ' '.      $right_string;
      }
  }
    $final .= '</ol>';
     echo $final;

输出

enter image description here