Add author avatar to posts
-
I would like to add the authors avatar to a post. I have successfully created my loop using the AJAX method of the plugin but can not seem to be able to call php to display the avatar or using AJAX (nothing in the documentation).
Is this supported?
Thanks!
-
Hi there!
Please share your code here, I lost my crystal ball so I can’t use my psychic abilities to see what you did over there š
Hi Hector, thanks for replying – sorry for not putting the code in the initial post!
Here is my code from the plugin…
<?php wpp_get_mostpopular( 'post_type="post"&range="all"&limit=4&excerpt_by_words=1&excerpt_length=55&thumbnail_width=758&thumbnail_height=600&stats_date=1&post_html=" <article class=\'one-fourth\' itemscope itemtype=\'http://schema.org/CreativeWork\'> <div class=\'post-grid-featured-image\'> <a href=\'{url}\' rel=\'bookmark\'>{thumb_img} </a> </div> <div class=\'entry-content\' itemprop=\'text\'> <h3 class=\'entry-title\' itemprop=\'headline\'><a href=\'{url}\' rel=\'bookmark\'>{text_title}</a></h3> </div> </article> "' ); ?>I would like to put the post authors avatar in there somewhere if possible.
Thanks!
While the wpp_get_mostpopular() template tag allows you to customize the HTML output, adding features that aren’t built-in into WPP (such as the author avatar) isn’t possible with it. For that, you will need to use either the wpp_custom_html filter hook or the wpp_post filter hook.
Here’s the same HTML output from your code, but using the wpp_post filter hook:
function custom_html_popular_post( $post_html, $p, $instance ){ $output = "<article class='one-fourth' itemscope itemtype='http://schema.org/CreativeWork'> <div class='post-grid-featured-image'> <a href='" . get_the_permalink($p->id) . "' rel='bookmark'>" . ( has_post_thumbnail($p->id) ? get_the_post_thumbnail($p->id) : '' ) . "</a> </div> <div class='entry-content' itemprop='text'> <h3 class='entry-title' itemprop='headline'><a href='" . get_the_permalink($p->id) . "' rel='bookmark'>" . get_the_title($p->id) . "</a></h3> </div> </article>"; return $output; } add_filter( 'wpp_post', 'custom_html_popular_post', 10, 3 );Since you didn’t mention it I’m guessing you want to display WordPress’ author avatar and for that you’ll need to use the get_avatar() function.
Thank you for your help! That’s great!
Here’s the code for others:
<div class='avatar'>" . get_avatar( get_the_author_meta('email'), 50) . "</div>Two questions though:
I am using Varnish on my server, hense the need for this to be Ajax. Will this still work okay with Ajax?
I also would like to display the post titles in widgets in the footer and the sidebar. The above changes it site wide. Is there a way to separate the two. The above is going on the homepage but also just the post titles are displayed using your widget.
Thanks again! š
OK, so here’s my code:
function homepage_grid( $post_html, $p, $instance ){ $output = '<article class="one-fourth post" itemscope itemtype="http://schema.org/CreativeWork"> <div class="post-grid-featured-image"><a href="' . get_the_permalink($p->id) . '" rel="bookmark">' . get_the_post_thumbnail( $p->id, 'small-post-image') . '</a></div> <div class="entry-content" itemprop="text"> <h3 class="entry-title" itemprop="headline"><a href="' . get_the_permalink($p->id) . '" rel="bookmark">' . $p->title . '</a></h3> <div class="entry-author-info"> <a href="' . get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ) . '">' . get_category_link($p->id) . '' . get_avatar( get_the_author_meta('email'), 50) . '</a><a href="' . get_author_posts_url( get_the_author_meta( 'ID' ) ) . '">' . get_the_author_meta( 'display_name' ) .'</a></div> </div> </article>'; return $output; } add_action( 'wpp_post', 'homepage_grid', 10, 3 );I am struggling to add the post category into this though which is a bit frustrating.
And as above, I have a list of posts in the footer widget and the sidebar widget. The above is to be displayed on the homepage, main content. Is there a way to separate these so the widgets have a list and homepage has the above? (One thing to note is that the homepage has a list in the footer AND the above, otherwise I’d just use an if statement to use this on the homepage only.
Any help would be great.
Thanks!
Hi there!
I am struggling to add the post category into this though which is a bit frustrating.
Here’s how I would do it:
function homepage_grid( $post_html, $p, $instance ){ $categories = get_the_category($p->id); $category_output = ''; if ( !empty($categories) ) { $category_output = 'Posted under '; foreach( $categories as $category ) { $category_output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s' ), $category->name ) ) . '" rel="category tag">' . esc_html( $category->name ) . '</a>, '; } $category_output = rtrim( $category_output, ', ' ); } $output = ' <article class="one-fourth post" itemscope itemtype="http://schema.org/CreativeWork"> <div class="post-grid-featured-image"><a href="' . get_the_permalink($p->id) . '" rel="bookmark">' . get_the_post_thumbnail( $p->id, 'small-post-image') . '</a></div> <div class="entry-content" itemprop="text"> <h3 class="entry-title" itemprop="headline"><a href="' . get_the_permalink($p->id) . '" rel="bookmark">' . $p->title . '</a></h3> <div class="entry-author-info"> <a href="' . get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ) . '">' . get_avatar( get_the_author_meta('email'), 50) . '</a><a href="' . get_author_posts_url( get_the_author_meta( 'ID' ) ) . '">' . get_the_author_meta( 'display_name' ) .'</a> </div> </div> <footer>' . $category_output . '</footer> </article>'; return $output; } add_action( 'wpp_post', 'homepage_grid', 10, 3 );I have a list of posts in the footer widget and the sidebar widget. The above is to be displayed on the homepage, main content. Is there a way to separate these so the widgets have a list and homepage has the above?
Yep. The
$instancevariable includes a key called widget_id ($instance['widget_id']) that you can use to give each widget a different output. If your theme uses the id parameter in before_widget, you will be able to see the widget_id in the HTML output of your sidebar/footer (eg.<article id="wpp-1"> ... </article>).Let’s say for example that your widgets have the IDs wpp-2 and wpp-3 being the former the one in the sidebar and the latter the one in the footer, and that you want the widget sidebar to use the “stock” HTML output while the one in the footer will have a custom HTML output:
function homepage_grid( $post_html, $p, $instance ){ // This is the sidebar widget, let's use the stock HTML output if ( 'wpp-2' == $instance['widget_id'] ) { return $post_html; } // If we got here, then use a custom HTML output $categories = get_the_category($p->id); $category_output = ''; if ( !empty($categories) ) { $category_output = 'Posted under '; foreach( $categories as $category ) { $category_output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s' ), $category->name ) ) . '" rel="category tag">' . esc_html( $category->name ) . '</a>, '; } $category_output = rtrim( $category_output, ', ' ); } $output = ' <article class="one-fourth post" itemscope itemtype="http://schema.org/CreativeWork"> <div class="post-grid-featured-image"><a href="' . get_the_permalink($p->id) . '" rel="bookmark">' . get_the_post_thumbnail( $p->id, 'small-post-image') . '</a></div> <div class="entry-content" itemprop="text"> <h3 class="entry-title" itemprop="headline"><a href="' . get_the_permalink($p->id) . '" rel="bookmark">' . $p->title . '</a></h3> <div class="entry-author-info"> <a href="' . get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ) . '">' . get_avatar( get_the_author_meta('email'), 50) . '</a><a href="' . get_author_posts_url( get_the_author_meta( 'ID' ) ) . '">' . get_the_author_meta( 'display_name' ) .'</a> </div> </div> <footer>' . $category_output . '</footer> </article>'; return $output; } add_action( 'wpp_post', 'homepage_grid', 10, 3 );Note that $instance[‘widget_id’] is only available to widgets. Neither the wpp_get_mostpopular() template tag nor the [wpp] shortcode have access to this variable.
Hi Hector,
Thank you very much for your help so far on this, you’ve been a massive help. The site I am trying to build is this; speakwp.com just so you can get an idea of what I’m trying to achieve.
The above code works great for the main homepage. However, I can not seem to get it to separate from the widgets (footer and sidebar) so they listen to the actual widget information.
I understand what you’re saying above, I just can’t seem to get it to do what I want it to do. So at the moment, the footer and post sidebar widget is trying to show the same information that’s on the homepage, instead of a normal list (no thumbnails etc).
Maybe I’m doing something wrong. I’ve set ID’s in the widgets to try use the if_statement, it just won’t listen š
Thanks again
The domain speakwp.com throws a 403 Forbidden error here, so I can’t see the site š
Ah, not sure why that is. maybe it’s not redirecting properly: http://www.speakwp.com
So, near the bottom of the homepage, theres ‘featured posts’ in the same style as the rest of the homepage.
Then in the footer widget and post/page sidebar I just want a list. it’s trying to show all the images still in the sidebar
Alright, let’s take advantage of what I posted earlier.
From what I see, you’re using the wpp_get_mostpopular() template tag on the Featured posts section and a widget on the footer. Remember when I said that
$instance['widget_id']is only available for widgets? Let’s take advantage of that:function homepage_grid( $post_html, $p, $instance ){ // This is the sidebar widget, let's use the stock HTML output if ( isset($instance['widget_id']) ) { return $post_html; } // If we got here, then it's the wpp_get_mostpopular() template tag and so let's use a custom HTML output $categories = get_the_category($p->id); $category_output = ''; if ( !empty($categories) ) { $category_output = 'Posted under '; foreach( $categories as $category ) { $category_output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s' ), $category->name ) ) . '" rel="category tag">' . esc_html( $category->name ) . '</a>, '; } $category_output = rtrim( $category_output, ', ' ); } $output = ' <article class="one-fourth post" itemscope itemtype="http://schema.org/CreativeWork"> <div class="post-grid-featured-image"><a href="' . get_the_permalink($p->id) . '" rel="bookmark">' . get_the_post_thumbnail( $p->id, 'small-post-image') . '</a></div> <div class="entry-content" itemprop="text"> <h3 class="entry-title" itemprop="headline"><a href="' . get_the_permalink($p->id) . '" rel="bookmark">' . $p->title . '</a></h3> <div class="entry-author-info"> <a href="' . get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ) . '">' . get_avatar( get_the_author_meta('email'), 50) . '</a><a href="' . get_author_posts_url( get_the_author_meta( 'ID' ) ) . '">' . get_the_author_meta( 'display_name' ) .'</a> </div> </div> <footer>' . $category_output . '</footer> </article>'; return $output; } add_action( 'wpp_post', 'homepage_grid', 10, 3 );At the beginning of the function we’ll be checking if
$instance['widget_id']is set or not – if it is, then it means that it’s the widget being loaded so let’s use the regular HTML output; if it’s not, then it means that the wpp_get_mostpopular() template tag is being called so let’s use your custom HTML output.Thank you for your help. Ok so I’ve included your code and on my homepage, I have
`<php Echo homepage_grid(‘output’); ?>I suspect this is incorrect as it doesn’t work lol but can’t find anything in the documentation to display code front end.
Thanks
No, no. You need to use the wpp_get_mostpopular() template tag. What I posted above goes into your theme’s functions.php file.
Thanks.
Okay, so entering this into functions.php:
function homepage_grid( $post_html, $p, $instance ){ // This is the sidebar widget, let's use the stock HTML output if ( isset($instance['widget_id']) ) { return $post_html; } // If we got here, then it's the wpp_get_mostpopular() template tag and so let's use a custom HTML output $categories = get_the_category($p->id); $category_output = ''; if ( !empty($categories) ) { $category_output = 'Posted under '; foreach( $categories as $category ) { $category_output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s' ), $category->name ) ) . '" rel="category tag">' . esc_html( $category->name ) . '</a>, '; } $category_output = rtrim( $category_output, ', ' ); } $output = ' <article class="one-fourth post" itemscope itemtype="http://schema.org/CreativeWork"> <div class="post-grid-featured-image"><a href="' . get_the_permalink($p->id) . '" rel="bookmark">' . get_the_post_thumbnail( $p->id, 'small-post-image') . '</a></div> <div class="entry-content" itemprop="text"> <h3 class="entry-title" itemprop="headline"><a href="' . get_the_permalink($p->id) . '" rel="bookmark">' . $p->title . '</a></h3> <div class="entry-author-info"> <a href="' . get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ) . '">' . get_avatar( get_the_author_meta('email'), 50) . '</a><a href="' . get_author_posts_url( get_the_author_meta( 'ID' ) ) . '">' . get_the_author_meta( 'display_name' ) .'</a> </div> </div> <footer>' . $category_output . '</footer> </article>'; return $output; } add_action( 'wpp_post', 'homepage_grid', 10, 3 );This in the location where I want the above to display on my homepage, inside the homepage template file:
<?php if (function_exists('wpp_get_mostpopular')) wpp_get_mostpopular(); ?>Literally just displays a list and view count of the popular posts. Instead of including the code that’s above with the thumbnail, category, author avatar etc.
The good thing is that now, the sidebar and footer widgets just display a list and nothing else, which is what I want.
I now just need to get the functions.php styling to appear when calling the wpp_get_mostpopular(); tag.
Am i missing something out of the above? Surely, i’d need to call the action on the homepage? Or can it just suss itself out?
Sorry to be annoying
Don’t worry about it.
I don’t see anything wrong, so I’m probably missing something as well. Try changing this bit:
// This is the sidebar widget, let's use the stock HTML output if ( isset($instance['widget_id']) ) { return $post_html; }into:
// This is the sidebar widget, let's use the stock HTML output if ( isset($instance['widget_id']) ) { echo 'widget_id is set<br>'; return $post_html; }Let me know when that’s done so I can go check what the output is.
The topic ‘Add author avatar to posts’ is closed to new replies.