Convert author name to link

Hi,

I would like to make the author’s name (in the post meta) an active link, linking to a page with all of the author’s posts. You helped me do this with an earlier version of X here:

Would it be possible for you to provide the corresponding code for the most recent version of X, and can you tell me where in the child theme’s functions.php the code should be added? Here is the current code.

Wordpress version: 4.8.1
X version: 5.1.1
Website: travelstudies.org

Thanks very much.

Hi there,

Thank you for writing in. As you may know, Our theme does not have the author link functionality out of the box. So customization was provided by us is to help you get what you need, but implementing it and maintaining the code will be out of our support scope and for further maintenance for new possible versions of the theme that may cause the customization break you will need to hire a developer.

So kindly consider the following code a guide to get you started on linking author names to the author page.

if ( ! function_exists( 'x_ethos_entry_meta' ) ) :
  function x_ethos_entry_meta() {

      $author = ' by <a href="'.  get_author_posts_url(get_the_author_meta( 'ID' )) . '">' . get_the_author() . '</a></span>';

    $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() )
    );

    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: &ldquo;%s&rdquo;", '__x__' ), $category->name ) )
                              . '"> '
                              . $category->name
                              . '</a>'
                              . $separator;
        }

        $categories_list = sprintf( '<span>In %s',
          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: &ldquo;%s&rdquo;", '__x__' ), $category->name ) )
                            . '"> '
                            . $category->name
                            . '</a>'
                            . $separator;
      }

      $categories_list = sprintf( '<span>In %s',
        trim( $categories_output, $separator )
      );
    }

    if ( comments_open() ) {
      $title  = get_the_title();
      $link   = get_comments_link();
      $number = get_comments_number();
      if ( $number == 0 ) {
        $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: &ldquo;%s&rdquo;', '__x__' ), $title ) ),
          __( 'Leave a Comment' , '__x__' )
        );
      } else if ( $number == 1 ) {
        $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: &ldquo;%s&rdquo;', '__x__' ), $title ) ),
          $number . ' ' . __( 'Comment' , '__x__' )
        );
      } else {
        $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: &ldquo;%s&rdquo;', '__x__' ), $title ) ),
          $number . ' ' . __( 'Comments' , '__x__' )
        );
      }
    } else {
      $comments = '';
    }

    $post_type           = get_post_type();
    $post_type_post      = $post_type == 'post';
    $post_type_portfolio = $post_type == 'x-portfolio';
    $no_post_meta        = x_get_option( 'x_blog_enable_post_meta' ) == 0;
    $no_portfolio_meta   = x_get_option( 'x_portfolio_enable_post_meta' ) == 0;

    if ( $post_type_post && $no_post_meta || $post_type_portfolio && $no_portfolio_meta ) {
      return;
    } else {
      printf( '<p class="p-meta">%1$s%2$s%3$s%4$s</p>',
        $categories_list,
        $author,
        $date,
        $comments
      );
    }

  }
endif;

You can add the code above to the functions.php of your Chlid Theme. For more information regarding the best practices on code customization kindly read this article.

The code suggested will work only for Ethos stack whcih is the one you have on your website.

Hope it helps.

That worked perfectly. Thank you very much!

You’re welcome.

I’m interested in doing exactly this but with Renew Stack. Is it possible for you to please share the code I might add to functions.php to do so?

Hi There @punitarice

For Renew stack, you can add the following code into your child theme’s functions.php file.

if ( ! function_exists( 'x_renew_entry_meta' ) ) :
  function x_renew_entry_meta() {

    //
    // Author.
    //

    $author = ' by <a href="'.  get_author_posts_url(get_the_author_meta( 'ID' )) . '">' . get_the_author() . ' </a></span>';


    //
    // 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() )
    );


    //
    // 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: &ldquo;%s&rdquo;", '__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: &ldquo;%s&rdquo;", '__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() );


	  $text = ( 0 == $number ) ? __( 'Leave a Comment', '__x__' ) : sprintf( _n( '%s Comment', '%s Comments', $number, '__x__' ), $number );


	  $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: &ldquo;%s&rdquo;', '__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.

Thank you – this is almost exactly what I was hoping for. However, I’m hoping you can help me correct an issue with how the metadata is displaying.

Presently it shows:
by Author Name Date / Category /

I would like it to display as:
by Author Name • Date • Category

Hi there,

That’s possible, and it’s only doable through CSS. Please add this to your global custom CSS.

.p-meta>span:after {
    content: "•";
    padding: 0 0.5em 0 0.45em;
}

Thanks!

I was able to correct it to my desired outcome by doing this:

/Meta/
.p-meta>span:before {
content: “|”;
padding: 0 0.5em 0 0.45em;
}
.p-meta>span:after {
content: “”;
padding: 0;
}

Please let me know if there is a more elegant or cleaner solution than what I’ve done.

Hey There,

This is the correct way of doing what you want to display for your post meta.
If you need anything else we can help you with, don’t hesitate to open another thread.

Regards.