对于一个项目与学生我使用WordPress木材(TWIG)ACF
在这个项目中,我创建了3种自定义帖子类型:论文
、主题强加
和主题自由
每个学生只能按自定义帖子类型创建一篇帖子(我为此创建了一个限制)。
但是现在我想显示一个列表,上面有每个学生的名字和他们的3篇文章。
像这样的列表:
尼古拉斯·马普尔
学位论文
自定义帖子类型名称图像(ACF字段)主题强加
自定义帖子类型名称图像(ACF字段)布兰达·史密斯
学位论文
自定义帖子类型名称图像(ACF字段)主题强加
自定义帖子类型名称图像(ACF字段)首先,我尝试获取每个学生的ID:
$students = get_users( array(
'role' => 'student',
'orderby' => 'user_nicename',
'order' => 'ASC',
'has_published_posts' => true
));
$students_id = array();
foreach ($students as $student) {
$students_id[] = $student->ID;
}
之后,从以下ID获取所有帖子:
$get_posts_students = get_posts( array(
'author' => $students_id,
'post_type' => array('dissertation', 'subject-imposed', 'subject-free')
));
$context['list_of_students'] = $get_posts_students;
我得到了一个错误urldecode(),它希望参数1是字符串和一个数组,但包含所有帖子,而不是按学生分组
能帮我点忙吗?如何按学生分组帖子?
随着get_posts
在Foreach我得到了帖子分组。但是我没有每组学生的名字。
$students = get_users( array(
'role' => 'student',
'orderby' => 'rand',
'has_published_posts' => true
));
$students_posts = array();
foreach ($students as $student) {
$students_posts[] = get_posts( array(
'author' => $student->ID,
'post_type' => array('dissertation', 'subject-imposed', 'subject-free')
));
}
$context['students_posts'] = $students_posts;
您可以使用get\u user\u meta()
将学生的姓名包含到数组中:
foreach ($students as $student) {
// Get the first name from user meta.
$first_name = get_user_meta( $student->ID, 'first_name');
// Get the last name from user meta.
$last_name = get_user_meta( $student->ID, 'last_name');
// Add a new array key for "name". The first name and last name return as arrays, so we use [0] to get the first index.
$students_posts['name'] = $first_name[0] . ' ' . $last_name[0];
$students_posts[] = get_posts( array(
'author' => $student->ID,
'post_type' => array('dissertation', 'subject-imposed', 'subject-free')
));
}
$context['students_posts'] = $students_posts;