【问题标题】:WordPress: get author info from post idWordPress:从帖子 ID 获取作者信息
【发布时间】:2011-03-12 11:03:23
【问题描述】:

甚至是来自帖子 ID 的作者 ID。我正在尝试在单个帖子页面的侧边栏中(在帖子循环之外)返回作者元(作者页面链接和头像)。做这个的最好方式是什么?我正在使用自定义函数(见下文)来返回帖子 ID,但不确定接下来要调用什么函数。

function this_post_id() {
  global $wp_query;
  $thePostID = $wp_query->post->ID;
  return $thePostID;
}

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我想通了。

    <?php $author_id=$post->post_author; ?>
    <img src="<?php the_author_meta( 'avatar' , $author_id ); ?> " width="140" height="140" class="avatar" alt="<?php echo the_author_meta( 'display_name' , $author_id ); ?>" />
    <?php the_author_meta( 'user_nicename' , $author_id ); ?> 
    

    【讨论】:

    • 酷 - 我将删除我的答案,因为它具有误导性。很高兴你把它分类了
    • 为了记录,使用 echo the_author_meta 是多余的,因为 the_author_meta 已经按原样回显了值。
    • 注意:您必须在 $author_id var 之前使用 global $post 才能使其工作。
    • 如果您想检索作者元数据而不立即回显它,请使用 get_the_author_meta( field, id ); codex.wordpress.org/Function_Reference/get_the_author_meta
    • get_avatar_url($author_id) 是获取作者头像图片 url 的更好方法。
    【解决方案2】:

    如果你想在循环之外使用下面的代码。

    <?php
    $author_id = get_post_field ('post_author', $cause_id);
    $display_name = get_the_author_meta( 'display_name' , $author_id ); 
    echo $display_name;
    ?>
    

    【讨论】:

      【解决方案3】:
      <?php
        $field = 'display_name';
        the_author_meta($field);
      ?>
      

      $field 参数的有效值包括:

      • admin_color
      • 瞄准
      • comment_shortcuts
      • 说明
      • display_name
      • 名字
      • 身份证
      • 胡言乱语
      • 姓氏
      • 昵称
      • plugins_last_view
      • plugins_per_page
      • rich_editing
      • syntax_highlighting
      • user_activation_key
      • 用户描述
      • user_email
      • 用户名
      • 用户姓氏
      • 用户级别
      • user_login
      • user_nicename
      • user_pass
      • user_registered
      • 用户状态
      • user_url
      • yim

      【讨论】:

        【解决方案4】:
        **use this code for display Auth Name**
        
        <?php
        $auth_id = $post->post_author; 
        echo get_the_author_meta( 'display_name', $auth_id ); 
        ?>
        

        【讨论】:

        • 虽然这个答案实际上是可以的,但如果您提供解释和支持证据会更有帮助
        猜你喜欢
        • 2013-10-11
        • 1970-01-01
        • 1970-01-01
        • 2011-03-25
        • 1970-01-01
        • 1970-01-01
        • 2010-12-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多