我需要尝试在 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>
他们的顺序是这样的,四年级在五年级之前。$assignment_fields['grade'][0]
将打印出fourthgrade
或fifthgrade
为循环中的每个项目。
有没有办法我可以找到它何时发生变化,所以第一次是fourthgrade
and fifthgrade
,所以我可以有这样的东西,而不是上面的列表:
<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>