-
AuthorPosts
-
March 7, 2015 at 7:01 pm #222438
Hi,
Is it possible to add the avatar of the author to each blog post in the meta, and also make the authors name ‘clickable’ so clicking it would display other posts by that author or show their buddypress profile page?URL: http://theafterword.co.uk
Using Renew Child theme. X 3.2.2
March 8, 2015 at 8:28 am #222656Hi There,
Thanks for writing in.
Because this requires a template change, I’d advise that you setup a child theme. This allows you to make code changes that won’t be overwritten when an X update is released. After your child theme is setup, please add following PHP code below into functions.php locates in child theme folder:// ============================================================================= // FUNCTIONS/RENEW.PHP // ----------------------------------------------------------------------------- // Renew specific functions. // ============================================================================= // ============================================================================= // TABLE OF CONTENTS // ----------------------------------------------------------------------------- // 01. Entry Meta // 02. Portfolio Tags // 03. Individual Comment // ============================================================================= // Entry Meta // ============================================================================= if ( ! function_exists( 'x_renew_entry_meta' ) ) : function x_renew_entry_meta() { // // Author. // $author = sprintf( '<a href="%s"><span>%s</span></a>', get_author_posts_url( get_the_author_meta( 'ID' ) ), get_the_author() ); // // Avatar. // $avatar = sprintf( '<a href="%s"><span>%s</span></a>', get_author_posts_url( get_the_author_meta( 'ID' ) ), get_avatar( get_the_author_meta( 'ID' ), 32 ) ); // // Date. // $date = sprintf( '<span><time class="entry-date" datetime="%1$s">%2$s</time></span>', esc_attr( get_the_date( 'c' ) ), esc_html( get_the_date( 'm.Y' ) ) ); // // Categories. // if ( get_post_type() == 'x-portfolio' ) { if ( has_term( '', 'portfolio-category', NULL ) ) { $categories = get_the_terms( get_the_ID(), 'portfolio-category' ); $separator = ', '; $categories_output = ''; foreach ( $categories as $category ) { $categories_output .= '<a href="' . get_term_link( $category->slug, 'portfolio-category' ) . '" title="' . esc_attr( sprintf( __( "View all posts in: “%s”", '__x__' ), $category->name ) ) . '"> ' . $category->name . '</a>' . $separator; } $categories_list = sprintf( '<span>%s</span>', trim( $categories_output, $separator ) ); } else { $categories_list = ''; } } else { $categories = get_the_category(); $separator = ', '; $categories_output = ''; foreach ( $categories as $category ) { $categories_output .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in: “%s”", '__x__' ), $category->name ) ) . '">' . $category->name . '</a>' . $separator; } $categories_list = sprintf( '<span>%s</span>', trim( $categories_output, $separator ) ); } // // Comments link. // if ( comments_open() ) { $title = apply_filters( 'x_entry_meta_comments_title', get_the_title() ); $link = apply_filters( 'x_entry_meta_comments_link', get_comments_link() ); $number = apply_filters( 'x_entry_meta_comments_number', get_comments_number() ); if ( $number == 0 ) { $text = __( 'Leave a Comment' , '__x__' ); } else if ( $number == 1 ) { $text = $number . ' ' . __( 'Comment' , '__x__' ); } else { $text = $number . ' ' . __( 'Comments' , '__x__' ); } $comments = sprintf( '<span><a href="%1$s" title="%2$s" class="meta-comments">%3$s</a></span>', esc_url( $link ), esc_attr( sprintf( __( 'Leave a comment on: “%s”', '__x__' ), $title ) ), $text ); } else { $comments = ''; } // // Output. // if ( x_does_not_need_entry_meta() ) { return; } else { printf( '<p class="p-meta">%1$s%2$s%3$s%4$s%5$s</p>', $avatar, $author, $date, $categories_list, $comments ); } } endif;
Hope it helps.
March 8, 2015 at 11:45 am #222736Thanks!, that works great.
I added spaces to this line:
printf( '<p class="p-meta">%1$s %2$s / %3$s%4$s%5$s</p>',
it seems to work but is that the right way to space the variables in the output?
March 8, 2015 at 8:00 pm #222909Hey there,
Yes, that is the way to add space.
Thanks.
March 22, 2016 at 4:31 pm #848598Hi, I am using Integrity and want to add similar functionality. How do I go about it? Thanks.
March 22, 2016 at 9:18 pm #848882Hi Victor,
Please use the following code instead. Add it on your child theme’s function.php file:
// Entry Meta // ============================================================================= if ( ! function_exists( 'x_integrity_entry_meta' ) ) : function x_integrity_entry_meta() { // // Author. // $author = sprintf( '<a href="%s"><span><i class="x-icon-pencil" data-x-icon=""></i> %s</span></a>', get_author_posts_url( get_the_author_meta( 'ID' ) ), get_the_author() ); // // Date. // $date = sprintf( '<span><time class="entry-date" datetime="%1$s"><i class="x-icon-calendar" data-x-icon=""></i> %2$s</time></span>', esc_attr( get_the_date( 'c' ) ), esc_html( get_the_date() ) ); // // Categories. // if ( get_post_type() == 'x-portfolio' ) { if ( has_term( '', 'portfolio-category', NULL ) ) { $categories = get_the_terms( get_the_ID(), 'portfolio-category' ); $separator = ', '; $categories_output = ''; foreach ( $categories as $category ) { $categories_output .= '<a href="' . get_term_link( $category->slug, 'portfolio-category' ) . '" title="' . esc_attr( sprintf( __( "View all posts in: “%s”", '__x__' ), $category->name ) ) . '"><i class="x-icon-bookmark" data-x-icon=""></i> ' . $category->name . '</a>' . $separator; } $categories_list = sprintf( '<span>%s</span>', trim( $categories_output, $separator ) ); } else { $categories_list = ''; } } else { $categories = get_the_category(); $separator = ', '; $categories_output = ''; foreach ( $categories as $category ) { $categories_output .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in: “%s”", '__x__' ), $category->name ) ) . '"><i class="x-icon-bookmark" data-x-icon=""></i> ' . $category->name . '</a>' . $separator; } $categories_list = sprintf( '<span>%s</span>', trim( $categories_output, $separator ) ); } // // Comments link. // if ( comments_open() ) { $title = apply_filters( 'x_entry_meta_comments_title', get_the_title() ); $link = apply_filters( 'x_entry_meta_comments_link', get_comments_link() ); $number = apply_filters( 'x_entry_meta_comments_number', get_comments_number() ); if ( $number == 0 ) { $text = __( 'Leave a Comment' , '__x__' ); } else if ( $number == 1 ) { $text = $number . ' ' . __( 'Comment' , '__x__' ); } else { $text = $number . ' ' . __( 'Comments' , '__x__' ); } $comments = sprintf( '<span><a href="%1$s" title="%2$s" class="meta-comments"><i class="x-icon-comments" data-x-icon=""></i> %3$s</a></span>', esc_url( $link ), esc_attr( sprintf( __( 'Leave a comment on: “%s”', '__x__' ), $title ) ), $text ); } else { $comments = ''; } // // Output. // if ( x_does_not_need_entry_meta() ) { return; } else { printf( '<p class="p-meta">%1$s%2$s%3$s%4$s</p>', $author, $date, $categories_list, $comments ); } } endif;
Hope this helps.
March 23, 2016 at 2:46 am #849160Hi,
It did do some of things I wanted but didn’t quite get me the view I wanted (see the current state image). Please see the other attached image. I want a circular author image along with other information but I am only getting author name and date. Also the meta data is all caps, I want it to be the way it looks in the image.
Thanks.
March 23, 2016 at 3:29 am #849193Hi Victor,
You can add this under Custom > CSS in the Customizer.
.p-meta { text-transform:none; }
Then change the code given above with this.
// Entry Meta // ============================================================================= if ( ! function_exists( 'x_integrity_entry_meta' ) ) : function x_integrity_entry_meta() { // // Author. // $author = sprintf( '<a href="%1$s"><span class="author-gravatar">%2$s</span><span> %3$s</span></a>', get_author_posts_url( get_the_author_meta( 'ID' ) ), get_avatar( get_the_author_meta( 'ID' ), 32 ), get_the_author() ); // // Date. // $date = sprintf( '<span><time class="entry-date" datetime="%1$s"><i class="x-icon-calendar" data-x-icon=""></i> %2$s</time></span>', esc_attr( get_the_date( 'c' ) ), esc_html( get_the_date() ) ); // // Categories. // if ( get_post_type() == 'x-portfolio' ) { if ( has_term( '', 'portfolio-category', NULL ) ) { $categories = get_the_terms( get_the_ID(), 'portfolio-category' ); $separator = ', '; $categories_output = ''; foreach ( $categories as $category ) { $categories_output .= '<a href="' . get_term_link( $category->slug, 'portfolio-category' ) . '" title="' . esc_attr( sprintf( __( "View all posts in: “%s”", '__x__' ), $category->name ) ) . '"><i class="x-icon-bookmark" data-x-icon=""></i> ' . $category->name . '</a>' . $separator; } $categories_list = sprintf( '<span>%s</span>', trim( $categories_output, $separator ) ); } else { $categories_list = ''; } } else { $categories = get_the_category(); $separator = ', '; $categories_output = ''; foreach ( $categories as $category ) { $categories_output .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in: “%s”", '__x__' ), $category->name ) ) . '"><i class="x-icon-bookmark" data-x-icon=""></i> ' . $category->name . '</a>' . $separator; } $categories_list = sprintf( '<span>%s</span>', trim( $categories_output, $separator ) ); } // // Comments link. // if ( comments_open() ) { $title = apply_filters( 'x_entry_meta_comments_title', get_the_title() ); $link = apply_filters( 'x_entry_meta_comments_link', get_comments_link() ); $number = apply_filters( 'x_entry_meta_comments_number', get_comments_number() ); if ( $number == 0 ) { $text = __( 'Leave a Comment' , '__x__' ); } else if ( $number == 1 ) { $text = $number . ' ' . __( 'Comment' , '__x__' ); } else { $text = $number . ' ' . __( 'Comments' , '__x__' ); } $comments = sprintf( '<span><a href="%1$s" title="%2$s" class="meta-comments"><i class="x-icon-comments" data-x-icon=""></i> %3$s</a></span>', esc_url( $link ), esc_attr( sprintf( __( 'Leave a comment on: “%s”', '__x__' ), $title ) ), $text ); } else { $comments = ''; } // // Output. // if ( x_does_not_need_entry_meta() ) { return; } else { printf( '<p class="p-meta">%1$s%2$s%3$s%4$s</p>', $author, $date, $categories_list, $comments ); } } endif;
Hope that helps.
March 23, 2016 at 5:55 am #849332This worked just fine. Awesome.
Appreciate your help.
March 23, 2016 at 6:01 am #849339Got one more thing to do, How to add space between avatar image and avatar name? See image.
March 23, 2016 at 6:32 am #849362Hi Victor,
To add the space in between the author image and name, add following CSS under Custom > CSS in the Customizer:
.p-meta .author-gravatar { margin-right: 10px; }
Adjust 10px to increase/decrease the spacing.
Thank you!
-
AuthorPosts