Archived topic
Elements - Author PHP won't display
5 replies · Started by Dwayne on October 15, 2019
<p>This post was written by <?php the_author(); ?> - The Author.</p>
<?php echo get_avatar( get_the_author_meta( 'ID' ));?>
I can't figure out why I can't get an output. Neither of those will work. I'm just trying to display the author name so that I can style it.
Displaying other post data (title, meta, etc) work just fine.
I'm sure I'm missing something obvious.
The above first line just outputs as:
"This post was written by - The Author."
The second line just outs the default avatar.
I'm using the 'HOOK' element. I have ensured that "Execute PHP" is checked.
I'm using a modified version of the 'Merch' template.
Thanks for any info!
Hi there,
which Hook are you using ?
Hi,
Was attempting various. I want it to display 'after_header' - and I was just adding to the "Single Post Entry Header" element that is added by the Merch template. That template already displays the title, categories, date.
Seems to only work if the HOOK selected is after_main_content or later?
Yeah those functions only work within the loop or inside the post content. Try this instead:
<?php
global $post;
$author_id=$post->post_author;
$author_name = get_the_author_meta( 'display_name', $author_id );
$author_avatar = get_avatar( $author_id, 50 );
printf( '<span class="hero-byline">This post was written by %1s</span><span class="hero-avatar">%2s</span>',
$author_name,
$author_avatar
);
?>
Yes! the layout here was throwing me for a ...loop.
On a (related) note, a slight typo on the doc for 'removing author' from a page in the Documentation:
https://docs.generatepress.com/article/generate_post_author_output/#remove-from-post-type
It should say "Another example is if you want to remove the AUTHOR on a specific post type, but keep it elsewhere"
Glad to be of help - and thanks for pointing out the typo! We'll get that fixed asap