0

while ループで最初に出現する文字列を見つけようとする必要があります。回避できるのであれば、各要素の最初の要素を見つけるために Jquery を使用したくありません。

while ( $teacher_assignment_query->have_posts() ) {
    $teacher_assignment_query->the_post();
    $assignment_fields = get_post_custom($post->ID);
    //print '$assignment_fields['title'][0] and stuff here
});

これは、このような割り当てのリストを出力します

<li class="assignments fourthgrade"><a href="#">Do worksheet 2-1</a></li>
<li class="assignments fourthgrade"><a href="#">Do worksheet 1-2</a></li>
<li class="assignments fourthgrade"><a href="#">Do worksheet 1-1</a></li>
<li class="assignments fifthgrade"><a href="#">Volunteer somewhere</a></li>
<li class="assignments fifthgrade"><a href="#">Finish science project</a></li>

5年生の前に4年生という順番です。またはループ内の各項目に対して$assignment_fields['grade'][0]出力されます。fourthgradefifthgrade

それがいつ変化するかを見つける方法はありますか?最初はfourthgradefifthgradeです。したがって、上記のリストの代わりに、次のようなものを使用できます。

<li class="heading">Fourth Grade</li> //new heading
<li class="assignments fourthgrade"><a href="#">Do worksheet 2-1</a></li>
<li class="assignments fourthgrade"><a href="#">Do worksheet 1-2</a></li>
<li class="assignments fourthgrade"><a href="#">Do worksheet 1-1</a></li>

<li class="heading">Fifth Grade</li> //new heading
<li class="assignments fifthgrade"><a href="#">Volunteer somewhere</a></li>
<li class="assignments fifthgrade"><a href="#">Finish science project</a></li>
4

2 に答える 2

2
$last_title = '';
while ( $teacher_assignment_query->have_posts() ) {
    $teacher_assignment_query->the_post();
    $assignment_fields = get_post_custom($post->ID);
    if($assignment_fields['grade']!=$last_title){
        echo '<li class="heading">'.$assignment_fields['grade'].'</li>';
        $last_title = $assignment_fields['grade'];
    }
    //print '$assignment_fields['title'][0] and stuff here
});
于 2013-08-29T22:28:47.493 に答える
1

一時変数を使用して現在の結果を前の結果と比較し、異なる場合はヘッダーを変更できます。
例(メタコード):

$previous_grade="";
while(conditions) {
    // some code to get your data
    [...]
    // Compare the current grade with the previous one
    $current_grade=$assignment_fields['grade'];
    if($current_grade!=$previous_grade) {
        print "<li class=\"heading\">$current_grade</li>";
    }
    // Go ahead with the list
    print $assignment_fields['title'][0] and other stuffs;
    // Update the temporary variable
    $previous_grade=$current_grade;
}
于 2013-08-29T22:30:44.720 に答える