0

I have a site that has multiple authors for a single post, requiring multiple author box bios after the article.

I previously used get_the_author_meta() to pull the user's social media hyperlinks as clickable icons beneath the author description, but when using a foreach loop with co-authors only the first author's social media links are used.

I've tried changing the arguments $author_id to $coauthor_id, but this does not seem to work. Using get_the_co_author just pulls all the css sprite icons without the hyperlinks.

I know the original get_the_author_meta() code works because it still functions on single author posts, I just can't seem to convert it to be compatible with co-authors.

<?php

$twitter  = get_the_author_meta('twitter', $author_id);
$facebook = get_the_author_meta('facebook', $author_id);
$googleplus = get_the_author_meta('googleplus', $author_id);
$linkedin = get_the_author_meta('linkedin', $author_id);
$instagram = get_the_author_meta('instagram', $author_id);
$youtube = get_the_author_meta('youtube', $author_id);
$email = get_the_author_meta('email', $author_id);

global $post;
$author_id=$post->post_author;
foreach( get_coauthors() as $coauthor ): ?>

<div class="author-box">
<div class="author-avatar">
<?php echo get_avatar( $coauthor->user_email, '96' ); ?>
</div>

<div class="author-description">
<h5 class="author vcard"><span class="fn"><a href="<?php echo get_author_posts_url( $coauthor->ID, $coauthor->user_nicename ); ?>"><?php echo $coauthor->display_name; ?></a></span></h5>
<p class="author-bio"><?php echo $coauthor->description; ?></p>
</div>

<div class="author-box-social-icons">
<?php   
        if(!empty($email)) {
echo '<a title="Email" href="mailto:'.$email.'" id="author-social-email"></a>';
}       
        if(!empty($instagram)) {
echo '<a title="Instagram" href="'.$instagram.'" id="author-social-instagram"></a>';
}       
        if(!empty($youtube)) {
echo '<a title="YouTube" href="'.$youtube.'" id="author-social-youtube"></a>';
}       
        if(!empty($linkedin)) {
echo '<a title="Follow me on LinkedIn" href="'.$linkedin.'" id="author-social-linkedin"></a>';
}               
        if(!empty($googleplus)) {
echo '<a title="Follow me on Google Plus" href="'.$googleplus.'" id="author-social-googleplus"></a>';
}                   
        if(!empty($twitter)) {
echo '<a title="Follow me on Twitter" href="https://twitter.com/'.$twitter.'" id="author-social-twitter"></a>';
}
        if(!empty($facebook)) {
echo '<a title="Follow me on Facebook" href="'.$facebook.'" id="author-social-facebook"></a>';
}       

?>
</div>  

<div class="clear"></div>
</div><!-- .entry-author co-author -->

<?php endforeach;?>
  • What is the output of var_dump( get_coauthors() ); ? – Sally CJ Jul 29 '18 at 0:30
  • The foreach loop outputs the author profile image, name, and description for each author that is listed for the post. However, the array of icons that should link to social media and email are conflicting with the current loop parameters. It is drawing from the get_the_author_meta data for only one author, instead of both individual authors. There should be a way to make the_author (singular) be coauthors (pl), but the plugin documentation doesn't seem to cover this from what I can access on the private wiki. – jcongerkallas1 Jul 29 '18 at 1:07
  • Check my answer. Hopefully it helps you. – Sally CJ Jul 29 '18 at 1:19
1

These are outside of the foreach loop:

$twitter  = get_the_author_meta('twitter', $author_id);
$facebook = get_the_author_meta('facebook', $author_id);
$googleplus = get_the_author_meta('googleplus', $author_id);
$linkedin = get_the_author_meta('linkedin', $author_id);
$instagram = get_the_author_meta('instagram', $author_id);
$youtube = get_the_author_meta('youtube', $author_id);
$email = get_the_author_meta('email', $author_id);

So (remove that, and) try using $coauthor->ID with get_the_author_meta() like this:

<div class="author-box-social-icons">
<?php   
        if( $email = get_the_author_meta( 'email', $coauthor->ID ) ) {
echo '<a title="Email" href="mailto:'.$email.'" id="author-social-email"></a>';
}       
        if( $instagram = get_the_author_meta( 'instagram', $coauthor->ID ) ) {
echo '<a title="Instagram" href="'.$instagram.'" id="author-social-instagram"></a>';
}       
        if( $youtube = get_the_author_meta( 'youtube', $coauthor->ID ) ) {
echo '<a title="YouTube" href="'.$youtube.'" id="author-social-youtube"></a>';
}       
        if( $linkedin = get_the_author_meta( 'linkedin', $coauthor->ID ) ) {
echo '<a title="Follow me on LinkedIn" href="'.$linkedin.'" id="author-social-linkedin"></a>';
}               
        if( $googleplus = get_the_author_meta( 'googleplus', $coauthor->ID ) ) {
echo '<a title="Follow me on Google Plus" href="'.$googleplus.'" id="author-social-googleplus"></a>';
}                   
        if( $twitter = get_the_author_meta( 'twitter', $coauthor->ID ) ) {
echo '<a title="Follow me on Twitter" href="https://twitter.com/'.$twitter.'" id="author-social-twitter"></a>';
}
        if( $facebook = get_the_author_meta( 'facebook', $coauthor->ID ) ) {
echo '<a title="Follow me on Facebook" href="'.$facebook.'" id="author-social-facebook"></a>';
}       

?>
</div>

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.