Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top