1

ループ外のアーカイブおよび作成者ページに作成者の役割を表示したい このコードを見つけ、ループ内で正常に動作 する Wordpress で作成者の役割を取得する

しかし、これをアーカイブおよび作成者ページに追加すると、という警告メッセージが表示されます

Warning: array_shift() expects parameter 1 to be array, null given in

これを解決するには?

4

1 に答える 1

2

これらの例では、COMPLETE ANSWER Here で指定した関数を使用しています。functions.php ファイルで:

function get_user_role($id)
{
    $user = new WP_User($id);
    return array_shift($user->roles);
}

アーカイブ ページで:

if(have_posts()) : while(have_posts()) : the_post();
    $aid = $post->post_author;
    echo get_the_author_meta('user_nicename', $aid).' | '.get_user_role($aid);
endwhile;endif;

作成者テンプレートに関しては、作成者テンプレートのWordpress Codex に多くの役立つ情報があります。次のようなことができます。

<?php
$curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
echo $curauth->user_nicename.' | '.get_user_role($curauth->ID);
?>
于 2012-05-29T19:24:05.440 に答える