简化我当前显示特色帖子的方式,当一篇帖子由正式成员提交时,会向帖子添加一个post\\u meta值,然后以不同的样式显示。
但是我想根据帖子作者,用户角色来做这件事。
因此,如果提交帖子的用户是“full\\u成员”,那么他们的帖子将自动显示,而无需查询post\\u meta。
我在归档模板循环中尝试过类似的方法,只是在包装div中添加了一个“featured listing”类,但我认为我没有正确地看待它。
<?php $user = wp_get_current_user();
if ( in_array( \'full_member\', (array) $user->roles ) ) { ?>
featured-listing
<?php } ?>
EDIT
第1部分已使用Nathan对课堂问题的原始答案解决,自那以后,他的答案已经扩展,这对我不起作用,因此我将有效的解决方案粘贴在下面://* If the post author is a full member, they get a featured listing
function which_class() {
return post_author_role_in( \'full_member\' ) ? \'featured-listing\' : \'regular-listing\';
}
//* Determine if the post author is in the $role
function post_author_role_in( $role ) {
return in_the_loop() ? user_role_in( get_the_author_meta( \'ID\' ), $role ) : false;
}
//* Determine if the $user_id is in the $role
function user_role_in( $user_id, $role ) {
return in_array( $role, ( new WP_User( $user_id ) )->roles );
}
Part 2 However I still need a solution for
Id仍然希望使用条件来完全隐藏模板中的某些元素,是否有IF condition 比如说IF post author is in X role, display this, else display this
. ?请告知:)