我有两种帖子类型:Teachers
和Students
。样本数据:
教师帖子类型:
Teacher A
Teacher B
Teacher C
和学生的帖子类型:
Student A
Student B
Student C
and many more students
我还有一个名为student-role
的分类法,它具有一些值:
Intern
Master Student
PhD Student
在学生职位类型中,我正在使用ACF关系将老师分配给学生。一个学生可以有多个老师。
例如:
Student A
是Intern
,并分配给Teacher A
和
Teacher B
Student B
是Master Student
,并分配给
Teacher C
Student C
是PhD Student
,并分配给Teacher C
我想为teacher C
创建一个自定义页面,其中仅显示所有student-role
。
因此,有一个针对C老师的页面,该页面应列出Master Student
和PhD Student
,因为该分类法下有两个学生。
我试图先让所有学生,然后让所有学生角色,但不确定下一步该怎么做,有什么想法?
$args = array(
'posts_per_page' => -1,
'post_type' => 'students',
'meta_query' => array(
array(
'key' => 'acf_relationship',
'value' => 'Manual ID OF Teacher',
'compare' => 'LIKE'
)
)
);
// get all posts for teacher with ID
$all_posts = get_posts( $args );
// get all student roles
$tax_terms = get_terms( 'student-role');
答案 0 :(得分:0)
您的自定义页面名为single-teacher.php吗?
检查此代码。
<?php
// the get_the_ID() will get the teacher's ID since you are in single-teacher.php
// $the_post will be the student object
// $the_posts is the student's object
$the_posts = get_field('acf_relationship', get_the_ID());
if( $the_posts ): ?>
<ul>
<?php foreach( $the_posts as $p ): // variable must NOT be called $post ?>
<li>
<!-- This will display the student title or name -->
<a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_title(
$p->ID ); ?></a>
<!-- This will display the student's taxononmy -->
<?php
$the_terms = get_the_terms( $p->ID, 'your_taxonomy_name' );
$t_url = '';
$t_name = '';
foreach ($the_terms as $t) {
$t_url = get_term_link( $t->slug, 'your_taxonomy_name');
$t_name = $t->name;
}
?>
<!-- Print student role list with link -->
<span>Your student roles: <a href="<?php echo $t_url; ?>">
<?php echo $t_name; ?></a>
</span>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
答案 1 :(得分:0)
注意:分配给学生的ACF字段应仅返回Post id而不是post对象(您可以通过编辑acf字段进行设置)
此处查询C老师(c老师的职位ID = 1662)
$args = array(
'numberposts' => -1,
'post_type' => 'student',
);
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post();
if( in_array('1662', get_field('assign')) ) : ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endif;
endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); ?>
在while循环中,您可以显示标题,角色等分类,内容等