Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #870579
    germanpulse
    Participant

    I am looking to add a link to the author’s email on blog posts at the top where the the author’s name and the post’s date already is. I know I will need to modify this in the function.php file in my child theme, but am having a difficult time getting this meta field to show up. I am using the integrity theme if that helps.

    So, to be clear, my vision is to have the following displayed at the top of the blog post, under the headline…
    By (author name) on (date) / Email Author (clickable email link)

    #870904
    Nabeel A
    Moderator

    Hi there,

    Thanks for writing in! To assist you with this issue, we’ll first need you to provide us with your URL. This is to ensure that we can provide you with a tailored answer to your situation. Once you have provided us with your URL, we will be happy to assist you with everything.

    #871792
    germanpulse
    Participant

    The site is being run on my local computer as it is still in development.

    #872488
    Paul R
    Moderator

    Hi,

    To achieve that, you can add this in your child theme’s functions.php file.

    
    function x_integrity_entry_meta() {
    
        //
        // Author.
        //
    
        $author = sprintf( 'By %s on ',
          get_the_author()
        );
    
        $authoremail = sprintf( '<span><a href="mailto:%1$s"> %2$s </a></span>',
            get_the_author_meta( 'user_email' ),
            get_the_author_meta( 'user_email' )
        );
        
        
        //
        // 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 ) )
                                  . '"><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: &ldquo;%s&rdquo;", '__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: &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</p>',
            $author,
            $date,
            $authoremail
          );
        }
    
      }
    

    Hope that helps.

    #873555
    germanpulse
    Participant

    That code works, but there are some extra modifications I would like to make that seems to break the code. Let me explain the best I can…

    I would like the final output on the post to display be as follows:
    By [author (this would be a link that would direct to listing of posts by the author)] on [date] / [email (ideally with the email/envelop icon displayed to the left)]

    #873561
    germanpulse
    Participant

    One extra note… I want the email link to say Email instead of having the actual email address written out

    #874000
    Christopher
    Moderator

    Hi there,

    Please update your code to :

    function x_integrity_entry_meta() {
    
        //
        // Author.
        //
    
        
          $author = sprintf( '<span><a href="%1$s">%2$s</a></span>',
          get_author_posts_url( get_the_author_meta( 'ID' ) ),
          get_the_author()
        );
    
        $authoremail = sprintf( '<span><a href="mailto:%1$s">Email <i class="x-icon-envelope" data-x-icon=""></i> </a></span>',
            get_the_author_meta( 'user_email' ),
            get_the_author_meta( 'user_email' )
        );
        
        
        //
        // 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 ) )
                                  . '"><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: &ldquo;%s&rdquo;", '__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: &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</p>',
            $authoremail,
            $author,
            $date
            
          );
        }
    
      }

    Please add the following code in Customize -> Custom -> CSS :

    p.p-meta span:nth-child(2):before {
        content: "By ";
    }
    p.p-meta span:nth-child(3):before {
        content: "On ";
    }
    .p-meta>span:after{
    content:" ";
    }
    

    Hope that helps.

    #914344
    germanpulse
    Participant

    Followup question regarding this… I am using this technique to also show Twitter usernames for authors, but as not everyone that publishes on my site has a Twitter account, is there a way to change the code to only display if the field in their profile includes one?

    #941469
    Darshana
    Moderator

    @germanpulse

    Please provide us with the link to your site and also post the code that you have tried, so that we can assist you accordingly.

    Thanks!

    #944597
    germanpulse
    Participant
    This reply has been marked as private.
    #978205
    Rue Nel
    Moderator

    Hello There,

    Thanks for providing the login credentials. I went ahead and changed the code. I have removed this block:

    $authortwitter = sprintf( '<span class="p-meta-social"><a href="http://twitter.com/%1$s">  <i class="x-icon-twitter" data-x-icon=""></i> %2$s </a></span>',
                 get_the_author_meta( 'twitter' ),
                 get_the_author_meta( 'twitter' )
            );

    and I have replace it with this block:

    $twitter = get_the_author_meta( 'twitter' );
    
        if ( $twitter ) {
    	$authortwitter = sprintf( '<span class="p-meta-social"><a href="http://twitter.com/%1$s">  <i class="x-icon-twitter" data-x-icon=""></i> %2$s </a></span>',
                 get_the_author_meta( 'twitter' ),
                 get_the_author_meta( 'twitter' )
            );	
        }

    The result is the this:

    #978603
    germanpulse
    Participant

    Awesome! Thanks so much!!!

    #978831
    Prasant Rai
    Moderator

    You are most welcome. 🙂

  • <script> jQuery(function($){ $("#no-reply-870579 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>