1

I'm trying to get the author's avatar, first name and last name and their bio(description).

Luckily I found out how to do do just that, using several functions from the WordPress codex. What I'm trying to do now is get the current author of the post in my foreach loop.

But what I noticed, I changed the author of a post I made myself earlier to someone else, but it did not change on the front end. It kept showing the original author and not the new modified one.

This is a part of my code just after the header:

<section class="page blog">
    <div class="container">
        <?php foreach($items as $item) { ?>
            <div class="row">
                <div class="col-md-7">
                    <div class="post-content">
                        <h3 class="post-title"><?php echo $item->post_title; ?></h3>
                        <?php echo str_replace('<p>', '<p class="post-content--alinea">', apply_filters('the_content', $item->post_content)); ?>
                        <p class="post-date-time">
                            <?php echo strftime('%A %d %B \'%y, %H:%M', strtotime($item->post_date)); ?>
                        </p>
                    </div>
                    <div class="post-author">
                        <div class="col-md-3">
                            <?php echo get_avatar(get_the_author_meta('ID'), 128, '', 'Profielfoto'); ?>
                            <p class="post-author--caption"><?php the_author_firstname(); echo ' '; the_author_lastname(); ?></p>
                        </div>
                        <div class="col-md-9">
                            <h3 class="post-author--bio-title">Let's meet!<?php /* the_author(); */ ?></h3>
                            <p class="post-author--bio"><?php the_author_meta('description'); ?></p>
                        </div>
                    </div>
                </div>

I think I'm missing something in the foreach loop. But I'm not sure what.

  • Still googling around, $item->post_author shows the right author ID, now how to get it to show that name... and description.. – Alexscended Mar 11 '16 at 11:30
  • Function get_the_author_meta. If used within The Loop, the user ID need not be specified, it defaults to current post author. A user ID must be specified if used outside The Loop. ~ WordPress say. I hope you know what I mean The Loop, not foreach loop. So you need to get the author ID. ie; $author_id = get_post_field( 'post_author', $item->ID ); Sorry for late respons. yes you are right,. Then you can use get_the_author_meta( 'user_email', $author_id ), to get email. etc, – Jevuska Mar 11 '16 at 11:32
0

Your issue is your foreach loop. Because you don't manually setup postdata, the current post object is not available in global scope, that is why you cannot make use of template tags or anything else for that matter that relies on the post object.

For get_the_author_meta() to work in your current situation, you will have to explicitely pass the current post author to the function. Something like this will work

get_the_author_meta( 'ID', $item->post_author );

To avoid this, and to make proper use of template tags, setup postdata. For this to work, you will need to use the $post global. Just remember to reset the $post global back to the main query's $post value

foreach ( $items as $post ) {
    setup_postdata( $post );

    // Use normal template tags
    the_title();
    the_content();
    echo get_the_author_meta( 'ID' );
}
wp_reset_postdata(); // VERY VERY IMPORTANT
  • Ah thank you that explains a lot! Yeah this template is build by someone else, I'm still learning much about PHP and the Wordpress functions. I just applied your code, and tested it while switching the author on a specific post, and that worked! :) – Alexscended Mar 14 '16 at 8:55

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.