按作者显示最新帖子的标题

时间:2013-04-08 作者:MidhuN

我有以下功能,我用它来显示div中的作者姓名、作者头像和作者传记。我需要显示作者最新帖子的标题。有人能帮忙吗?

function ajaxified_function() 
{ 
    $response = new WP_Ajax_Response();
    $id        = $_POST[\'author_id\'];
    $auth_name = get_the_author_meta(\'display_name\', $id);
    $avatar    = get_avatar($id);
    $desc      = get_the_author_meta(\'description\',$id);
    $auth_desig  = get_the_author_meta(\'designation\', $id);
    $output = "<div id=\'bloggers_title\'>$auth_name</div>\\n
           <div id=\'bloggers_desig\'>$auth_desig</div>\\n
           <div id=\'bloggers_avatar\'>$avatar</div>\\n
           <div id=\'bloggers_desc\'>$desc</div>\\n";
    $response->add(array(
    \'what\' => \'has\',
    \'data\' => $output
)); 
$response->send();
}

1 个回复
最合适的回答,由SO网友:Sunyatasattva 整理而成

您可以获得作者向函数中添加以下代码的最新帖子:

$latest_post = get_posts( array(
        \'author\'      => $id,
        \'orderby\'     => \'date\',
        \'numberposts\' => 1
));

// Since get_posts() returns an array, but we know we only
// need one element, let\'s just get the element we need.
$latest_post = $latest_post[0];
然后修改$output, 添加所需的数据(特别是guid 对于permalink,以及post_title 对于标题),例如:

$output .= "<div id=\'bloggers_latest_post\'>
                <a href=\'$latest_post->guid\'>$latest_post->post_title</a>
            </div>"

结束