Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have created a page template for the front page of a site I am building using WordPress. I am displaying the 3 latest posts on this page and all works fine except for the display of the author of the post. I am using the following code

<?php
        $recentposts=get_posts('showposts=3');
        if ($recentposts) 
        {
            foreach($recentposts as $post)
            {
                //setup_postdata($post);
    ?>

        <div class="fifthFloat">
            <h3><a href="<?php the_permalink() ?>" rel="bookmark" 
                     title="Link to <?php the_title_attribute(); ?>">
                        <?php the_title(); ?></a></h3>
            <p>
            <?php
                $postContent = $post->post_content;
                if(strlen($postContent) > 50)
                {
                    $postContent = substr($postContent, 0, 
                             strrpos(substr($postContent, 0, 60), " ")) . " ...";
                }
                echo $postContent;
            ?>
            </p>
            <p>Posted <?php the_time("jS M Y"); ?> by <?php the_author(); ?></p>
        </div>

    <?php
            }
        }
    ?>

and I have also tried the following

$user_info = get_userdata($post->post_author);
echo $user_info->first_name;
echo $user_info->last_name;

but both result in an empty string.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Any reason this is commented out?

//setup_postdata($post);

This function setups global variables for current post and author data is pulled from one of those.

share|improve this answer
1  
hahaha now I really feel like an idiot!!! Thank you –  Simon Fox Dec 16 '10 at 8:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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