1

I have the following function which I use to show the author name,author avatar and the author biography in a div. I need to show the title of the latest post by the author along with this. Can anyone help?

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();
}
4

You can get the latest post of an author adding the following code to your function:

$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];

Then modify your $output, adding the data you need (specifically guid for the permalink, and post_title for the title), for example:

$output .= "<div id='bloggers_latest_post'>
                <a href='$latest_post->guid'>$latest_post->post_title</a>
            </div>"
  • Thank you very much....That was a great help you did.... – MidhuN Apr 8 '13 at 13:28
  • If this as solved your problem, you can accept the answer and thus closing the issue. – Sunyatasattva Apr 8 '13 at 13:34
  • Hi.. I have a small problem in the ajax question. Can you please help me. Till today morning it was working. But after that wen I saw suddenly it was not working. I think it is where I register the js and using the WP_localize_script. Can you please help me with that? – MidhuN Apr 9 '13 at 15:11
  • You know that things don't automatically stop working in code, right? Did you use version control? Can you go back and see what is the problem? Are you debugging? What errors does it give? How am I supposed to help you with such a general question? – Sunyatasattva Apr 10 '13 at 8:26
  • I have solved the problem... It was a conflict with one of the plugins. Thanks... – MidhuN Apr 10 '13 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.