我是否有办法从这个foreach循环中的列表中排除一位教师(“Robert”)并将其条目分别放在顶部?
$teachers = get_terms('teacher');
foreach($teachers as $teacher) {
$link = get_term_link(intval($teacher->term_id),'teacher');
$description = term_description(intval($teacher->term_id),'teacher');
echo "<h3><a href=\"{$link}\">{$teacher->name}</a></h3>";
echo substr($description,0,200) . "...";
}
'老师'是一个Wordpress分类术语。我正在尝试创建一个所有教师的名单,其中罗伯特是顶级老师。
答案 0 :(得分:2)
由于显而易见的原因,这个解决方案是浪费的,例如你可以将内部细节放在一个部分以保存重复两次的回声代码(不要重复自己,干)。修改生成查询也会更好,因此老师总是第一次得到结果,但是对于您使用的框架可能无法实现这一点。
最好使用抽象的#34老师&#34;要检查的值,而不是对名称进行硬编码。
$teachers = get_terms('teacher');
foreach($teachers as $teacher) {
if($teacher->name == 'Robert') {
$link = get_term_link(intval($teacher->term_id),'teacher');
$description = term_description(intval($teacher->term_id),'teacher');
echo "<h3><a href=\"{$link}\">{$teacher->name}</a></h3>";
echo substr($description,0,200) . "...";
}
}
foreach($teachers as $teacher) {
if($teacher->name != 'Robert') {
$link = get_term_link(intval($teacher->term_id),'teacher');
$description = term_description(intval($teacher->term_id),'teacher');
echo "<h3><a href=\"{$link}\">{$teacher->name}</a></h3>";
echo substr($description,0,200) . "...";
}
}