web-dev-qa-db-ja.com

投稿者の役割がXの場合

私が現在注目している投稿をどのように表示するかを単純化して、投稿がフルメンバーによって投稿されたとき、post_meta値が投稿に追加され、それが異なるスタイルになります。

しかしIdは、投稿者、代わりにユーザーロールに基づいてそれをやりたいのです。

そのため、投稿を送信したユーザーが "full_member"の場合、その投稿は自動的に紹介され、post_metaに問い合わせる必要はありません。

私はこのようなアーカイブテンプレートのループの中にあるものを試してみて、そしてラップしているdivに単に "feature-listing"クラスを追加していますが、私はそれを正しい方法で見ているとは思いません。

 <?php $user = wp_get_current_user();
if ( in_array( 'full_member', (array) $user->roles ) ) { ?>

featured-listing

<?php } ?>

_編集_

パート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 );
}

その2私はまだ解決策が必要です

Idはまだテンプレート内の特定の要素を完全に隠すために条件付き を使用したいのですが、 IF条件 IF post author is in X role, display this, else display thisのようなものです。 ?

お知らせ下さい :)

1
Randomer11

そのための1つの方法は、get_userdata()Codex で作者データを取得し、その役割をチェックすることです。

そのためにはユーザーIDが必要になるでしょう。そしてget_post_field()Codex でそれを得ることができます。

get_post_field()を使用するには投稿IDが必要になりますので、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;

もっと早くて簡単な方法はありますか? はい

それで、なぜ私はあなたにこれを「世界中で」解決策にしましたか? 私の経験から、この方法であなたが後であなたの結果を台無しにするかもしれない多くのものを防ぐことができるので -

それで、それをする別の方法はどうですか? それをするにはいくつかの方法があるので、あなたは例えばglobal $authordataを使うことができます。あなたはWordPressが持っているグローバルをチェックすることができます here

正しい方向に進んでいると思います。私が違うやり方をする唯一のことは、プレゼンテーションをロジックから切り離すことです。だから、あなたは持っているでしょう:

テンプレートファイル内:

<div class="<?php echo which_class(); ?>">Content</div>
<div class="<?php echo is_member() ? 'featured-listing' : '' ; ?>">Content</div>

functions.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_membersによる投稿を特徴としないため、使用したくないことに注意してください。

3
Nathan Johnson