If post author role is X

时间:2017-11-05 作者:Randomer11

简化我当前显示特色帖子的方式,当一篇帖子由正式成员提交时,会向帖子添加一个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. ?

请告知:)

2 个回复
最合适的回答,由SO网友:Marcelo Henriques Cortez 整理而成

一种方法是获取作者数据并检查其角色get_userdata() Codex

为此,您需要用户ID,您可以使用get_post_field() Codex

您需要帖子ID才能使用get_post_field(), 所以您可以使用get_queried_object_id()Codex

所以,你会有这样的想法:

//gets the ID of the post
$post_id = get_queried_object_id();

//gets the ID of the author using the ID of the post
$author_ID = get_post_field( \'post_author\', $post_id );

//Gets all the data of the author, using the ID
$authorData = get_userdata( $author_ID );

//checks if the author has the role of \'subscriber\'
if (in_array( \'subscriber\', $authorData->roles)):
    //show the content for subscriber role
else:
    //show the content for every other role
endif;
有没有更快/更简单的方法?Yes

那我为什么给你这个“环游世界”的解决方案呢?Because from my experience, this way you can prevent a lot of stuff that might mess up with your result later on

那么,另一种方法呢?Since there are a few ways to do it, you can for example use the global $authordata. You can check the globals WordPress has here

SO网友:Nathan Johnson

我认为你的做法是对的。唯一不同的是,我会将演示与逻辑分开。因此,您必须:

在模板文件中:

<div class="<?php echo which_class(); ?>">Content</div>
<div class="<?php echo is_member() ? \'featured-listing\' : \'\' ; ?>">Content</div>
在您的功能中。php:

//* Return whether the post author is a member
//* This function will only work in the loop
function is_member() {
  return post_author_role_in( \'full_member\' );
}

//* If the post author is a full member, they get a featured listing
//* This function will only work in the loop
function which_class() {
  return is_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_id(), $role ) : false;
}

//* Get the author ID
function get_the_author_id() {
  return get_the_author_meta( \'ID\' );
}

//* 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 );
}
注意,您不想使用wp_get_current_user() 因为这将只包含当前用户的帖子,而不是所有full\\u成员的帖子。

结束

相关推荐