[Resolved] Template Tags in Hooks

Home Forums Support Template Tags in Hooks

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #940091
    Clayton

    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.

    #940310
    David
    Staff
    Customer Support

    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>';
    ?>
    #940443
    Clayton

    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

    #940459
    David
    Staff
    Customer Support

    Try switching out $author_id for 'ID'

    #940462
    Clayton

    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.

    #940485
    David
    Staff
    Customer Support

    Daft question – what is the display name set to in the User Profile of the post author?

    #940493
    Clayton

    Good thing to check: https://imgur.com/a/T2JVd1l

    I think that is right…

    #940902
    Clayton

    Any other thoughts on what my causing the issue here? Let me know if you need login to take a look.

    #941127
    Tom
    Lead Developer
    Lead Developer

    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.

    #941943
    Clayton

    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>
    
    #941960
    David
    Staff
    Customer Support

    Looks good to me! Thanks for sharing

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.