How to add author image before author name?
-
See the screenshot for clarification and let me know how to do this. I also want to know how I can change the size of the image. Screenshot
- This topic was modified 4 years, 10 months ago by Rakib Hassan.
-
Maybe so, but I’m not sure.
functions.phpif ( ! function_exists( 'astra_post_author' ) ) { /** * Function to get Author of Post * * @param string $output_filter Filter string. * @return html Markup. */ function astra_post_author( $output_filter = '' ) { ob_start(); echo '<span class="my_class">'; echo get_avatar( $user_ID, 30 ); echo '</span>'; echo '<span '; echo astra_attr( 'post-meta-author', array( 'class' => 'posted-by vcard author', ) ); echo '>'; // Translators: Author Name. ?> <a title="<?php printf( esc_attr__( 'View all posts by %1$s', 'astra' ), get_the_author() ); ?>" href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>" rel="author" <?php echo astra_attr( 'author-url', array( 'class' => 'url fn n', ) ); ?> > <span <?php echo astra_attr( 'author-name', array( 'class' => 'author-name', ) ); ?> ><?php echo get_the_author(); ?></span> </a> </span> <?php $output = ob_get_clean(); return apply_filters( 'astra_post_author', $output, $output_filter ); } }
CSS
.my_class img {border-radius: 50%; margin-left: 3px;}
Astra theme developers will tell you better.
orif ( ! function_exists( 'astra_post_author' ) ) { /** * Function to get Author of Post * * @param string $output_filter Filter string. * @return html Markup. */ function astra_post_author( $output_filter = '' ) { ob_start(); echo '<span '; echo astra_attr( 'post-meta-author', array( 'class' => 'posted-by vcard author', ) ); echo '>'; // Translators: Author Name. ?> <a title="<?php printf( esc_attr__( 'View all posts by %1$s', 'astra' ), get_the_author() ); ?>" href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>" rel="author" <?php echo astra_attr( 'author-url', array( 'class' => 'url fn n', ) ); ?> > <span <?php echo astra_attr( 'author-name', array( 'class' => 'author-name', ) ); ?> ><?php echo get_the_author(); ?></span> </a> </span> <?php echo '<span class="my_class">'; echo get_avatar( $user_ID, 30 ); echo '</span>'; ?> <?php $output = ob_get_clean(); return apply_filters( 'astra_post_author', $output, $output_filter ); } }
Before “By”
if ( ! function_exists( 'astra_get_post_meta' ) ) { /** * Post meta * * @param string $post_meta Post meta. * @param string $separator Separator. * @return string post meta markup. */ function astra_get_post_meta( $post_meta, $separator = '/' ) { $output_str = ''; $loop_count = 1; $separator = apply_filters( 'astra_post_meta_separator', $separator ); foreach ( $post_meta as $meta_value ) { switch ( $meta_value ) { case 'author': $author = get_the_author(); if ( ! empty( $author ) ) { $output_str .= ( 1 != $loop_count && '' != $output_str ) ? ' ' . $separator . get_avatar( $user_ID, 30 ) . ' ' : ''; $output_str .= esc_html( astra_default_strings( 'string-blog-meta-author-by', false ) ) . astra_post_author(); } break; case 'date': $output_str .= ( 1 != $loop_count && '' != $output_str ) ? ' ' . $separator . ' ' : ''; $output_str .= astra_post_date(); break; case 'category': $category = astra_post_categories(); if ( '' != $category ) { $output_str .= ( 1 != $loop_count && '' != $output_str ) ? ' ' . $separator . ' ' : ''; $output_str .= $category; } break; case 'tag': $tags = astra_post_tags(); if ( '' != $tags ) { $output_str .= ( 1 != $loop_count && '' != $output_str ) ? ' ' . $separator . ' ' : ''; $output_str .= $tags; } break; case 'comments': $comment = astra_post_comments(); if ( '' != $comment ) { $output_str .= ( 1 != $loop_count && '' != $output_str ) ? ' ' . $separator . ' ' : ''; $output_str .= $comment; } break; default: $output_str = apply_filters( 'astra_meta_case_' . $meta_value, $output_str, $loop_count, $separator ); } $loop_count ++; } return $output_str; } }
add class
if ( ! function_exists( 'astra_get_post_meta' ) ) { /** * Post meta * * @param string $post_meta Post meta. * @param string $separator Separator. * @return string post meta markup. */ function astra_get_post_meta( $post_meta, $separator = '/' ) { $output_str = ''; $loop_count = 1; $separator = apply_filters( 'astra_post_meta_separator', $separator ); foreach ( $post_meta as $meta_value ) { switch ( $meta_value ) { case 'author': $author = get_the_author(); if ( ! empty( $author ) ) { $output_str .= ( 1 != $loop_count && '' != $output_str ) ? ' ' . $separator . get_avatar( $user_ID, 30, '', '', array('class'=>'my_class') ) . ' ' : ''; $output_str .= esc_html( astra_default_strings( 'string-blog-meta-author-by', false ) ) . astra_post_author(); } break; case 'date': $output_str .= ( 1 != $loop_count && '' != $output_str ) ? ' ' . $separator . ' ' : ''; $output_str .= astra_post_date(); break; case 'category': $category = astra_post_categories(); if ( '' != $category ) { $output_str .= ( 1 != $loop_count && '' != $output_str ) ? ' ' . $separator . ' ' : ''; $output_str .= $category; } break; case 'tag': $tags = astra_post_tags(); if ( '' != $tags ) { $output_str .= ( 1 != $loop_count && '' != $output_str ) ? ' ' . $separator . ' ' : ''; $output_str .= $tags; } break; case 'comments': $comment = astra_post_comments(); if ( '' != $comment ) { $output_str .= ( 1 != $loop_count && '' != $output_str ) ? ' ' . $separator . ' ' : ''; $output_str .= $comment; } break; default: $output_str = apply_filters( 'astra_meta_case_' . $meta_value, $output_str, $loop_count, $separator ); } $loop_count ++; } return $output_str; } }
CSS
.my_class {border-radius: 50%; margin-left: 3px;}
Hi @zabavljaev , thanks for your try. I wanted to show the image exactly before “By”. Your attached codes to the first reply worked out but didn’t load the avatar(showed default avatar once before and then after the author name). Your codes in the next replies didn’t work at all. Can you help me out to show the author’s image before “By” with avatar loaded +css class? I had set gravatar for the avatar. (pardon any typos grammatical error)
Regards
@rakibhassanThis is not my code. These are the functions of the Astra theme. I just added the output of the avatar. To display the gravatar you need to add get_the_author_meta (‘user_email’). In the last code (it works for me), replace in this part of the code:
case 'author': $author = get_the_author(); if ( ! empty( $author ) ) { $output_str .= ( 1 != $loop_count && '' != $output_str ) ? ' ' . $separator . get_avatar( get_the_author_meta('user_email'), 30, '', '', array('class'=>'my_class') ) . ' ' : ''; $output_str .= esc_html( astra_default_strings( 'string-blog-meta-author-by', false ) ) . astra_post_author(); } break;
But Astra theme developers have suggested better. I’m just an amateur.
Screenshot
- The topic ‘How to add author image before author name?’ is closed to new replies.