2015-11-04 36 views
0

我從我的查詢結果如下:
組結果由VAR從字符串

Techn1

  • P1一些文字

Techn2

  • 點P22一些文字

Tech3車隊

  • P1一些其他文本

Techn2

  • P52一些文字

Techn2

  • P62一些文字

我想組由Tech#的方式的結果,其結果將是

Techn1

  • p1部分文字

Techn2

  • P22一些文字

  • P52一些文字

  • P62一些文字

點Tech3車隊

  • P1一些其他文本

這是所用的代碼:

$vars = new WP_Query(array('category_name' => 'actualite')); 
     while ($vars->have_posts()) : $vars->the_post(); 
     $child_posts = types_child_posts("contenu");  
     foreach ($child_posts as $child_post) : 
        if($child_post->fields['theme']=='A'): 
        $technique =$child_post->fields['f1']; 
         if (@$lastTechnique != $technique): 
          print "<strong>".$technique."</strong><br>"; 
         endif;        
          echo "- ".$child_post->fields['f2']." Some text : ".$child_post->fields['f3']." (some text : ";the_title();echo ")<br>"; 
        else: 
         echo ""; 
        endif; 
       endforeach; 
     endwhile; 

回答

-1
$vars = new WP_Query(array('category_name' => 'actualite')); 
$techniques = []; 
while ($vars->have_posts()) : 
    $vars->the_post(); 
    $child_posts = types_child_posts("contenu");  
    foreach ($child_posts as $child_post) : 
     if($child_post->fields['theme']=='A'): 
      $technique =$child_post->fields['f1']; 
      if (!isset($techniques[$technique])) 
      { 
       $techniques[$technique] = []; 
      } 
      $techniques[$technique][] = "- ".$child_post->fields['f2']." Some text : ".$child_post->fields['f3']." (some text : " . get_the_title() . ")<br>"; 
     endif; 
    endforeach; 
endwhile; 
foreach ($techniques as $technique=>$childTexts): 
    print "<strong>".$technique."</strong><br>"; 
    foreach (array_unique($childTexts) as $childText): 
     print $childText; 
    endforeach; 
endforeach; 
+0

親愛的埃文,THX的嘗試。它會返回比預期更多的結果,因爲很多結果行會在輸出中被複制多次。代碼中放置foreach的地方肯定有問題。 – user5526048

+0

你能告訴我現在的輸出嗎?你使用第一個還是第二個例子? –

+0

nm我看到這個問題..它是用'the_title()'。發佈修正... –