Is there a conditional tag for latest post or do i need a query?

I need to display an avatar in the post info position which i have written the code for but i only want to display it on the most recent post on the home page and not a specific post as this will change when a new post id published.

Couldn’t find a conditional tag which handles this.

I’m using the code in a custom function with genesis_hook in the child theme.

function latest_post_author_avatars() {
if (is_single() || is_home() ) {  
echo get_avatar(get_the_author_id(), 40);

  }

}
add_action('genesis_after_post_title', 'latest_post_author_avatars');

// Here’s the final solution which works. Thanks to both of you.

function latest_post_author_avatars() {
global $wp_query;
if (is_single() || is_home() && 0 === $wp_query->current_post)  {
echo get_avatar(get_the_author_id(), 40);

  }

}
add_action('genesis_after_post_title', 'latest_post_author_avatars');

2 Answers
2

Within an ordinary WordPress Loop, the following would identify the first post in the Loop, which would be the latest if posts are ordered by date descending.

global $wp_query; // might not be necessary; depends on context
if (is_paged() && 0 === $wp_query->current_post) {
  echo 'first-post';
}

I don’t know how your custom function works as you didn’t post any code and I don’t how genesis_hook works as I don’t use Genesis, but hopefully that will help.

Leave a Comment