Archived topic
Template Tags in Hooks
10 replies · Started by Clayton on June 24, 2019
Is it possible to allow template tags to work within hooks? I've run into this several times where it would be super helpful if template tags worked within hooks...not just headers. The main issue I'm having though is that I'm trying to simply display the author name but can't get it to display within the above content hook.
This is the code I'm using that isn't working. I've got the avatar and date showing but can't get the author name to display. <?php get_the_author_meta( 'display_name', $author_id ); ?>
Any help would be greatly appreciated.
Hi there,
not currently, as i am not sure it is possible to add the template tags to hooks.
So the function needs to be echoed out like this:
<?php echo get_the_author_meta('display_name', $author_id); ?>
This will just output the string. So i you want to give it some style then do this instead:
<?php
$custom_author_name = get_the_author_meta('display_name', $author_id);
echo '<span class="custom-author">' . $custom_author_name . '</span>';
?>
Thanks for the reply David. That still doesn't seem to be working. Here is a screenshot of what I have. https://imgur.com/a/usZfAZH
Try switching out $author_id for 'ID'
Weird. Still nothing...man this is so strange. It's a very clean install of WordPress too so I don't think there is anything conflicting.
Daft question - what is the display name set to in the User Profile of the post author?
Good thing to check: https://imgur.com/a/T2JVd1l
I think that is right...
Any other thoughts on what my causing the issue here? Let me know if you need login to take a look.
You have a couple of issues there.
$id_or_email and $author_id aren't defined anywhere.
get_avatar() should look like this:
Then you should remove $author_id from the other get_the_author_meta() function.
This seems to be working from me. If you guys seen any red flags let me know.
<?php
global $post;
$author_id=$post->post_author;
?>
<div class="custom-post-sub-heading">
<h2><?php single_post_title(); ?></h2>
<h4><?php the_excerpt(); ?></h4>
<div class="custom-post-meta">
<div class="post-avatar"><?php echo get_avatar( $author_id, $size = '70'); ?></div>
<span class="author-name"><a href="<?php echo get_author_posts_url($author_id); ?>"><?php the_author_meta( 'display_name', $author_id ); ?></a></span><br>
<span class="entry-date"><?php echo get_the_date(); ?></span>
<span class="read-time">[kt_reading_time]</span>
</div>
</div>
Looks good to me! Thanks for sharing