Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1388233

    ingonuts
    Participant

    Hello,

    I’d like to include the reading time of an article in the meta information below the post title. The plugin I used generated the following code:

    < ?php post_read_time(); ? >

    How do I include it?

    #1388526

    Paul R
    Moderator

    Hi,

    Thanks for writing in!

    Which stack are you using?

    Can you provide us your site url so we can take a closer look.

    Thanks

    #1388886

    ingonuts
    Participant
    This reply has been marked as private.
    #1388924

    ingonuts
    Participant

    Ideally I would like the post title and meta to look like this

    POST TITLE
    Date – Minutes to Read – by Author – Comments

    #1389003

    Rupok
    Member

    Hi there,

    Thanks for writing back. You need to add and modify the below code within Child Theme’s functions.php :

    // Entry Meta
    // =============================================================================
    
    if ( ! function_exists( 'x_ethos_entry_meta' ) ) :
      function x_ethos_entry_meta() {
    
        //
        // Author.
        //
    
        $author = sprintf( ' %1$s %2$s</span>',
          __( 'by', '__x__' ),
          get_the_author()
        );
    
        //
        // 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>%1$s %2$s',
              __( 'In', '__x__' ),
              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>%1$s %2$s',
            __( 'In', '__x__' ),
            trim( $categories_output, $separator )
          );
        }

    Note that you need to re-order the codes and place your custom code there before copy pasting.

    Hope this helps.

    #1389063

    ingonuts
    Participant

    Thank you, when I paste this into the functions.php it give me an unexpected end of file error?

    Also, where would I place the < ?php post_read_time(); ? > at the end?

    #1389312

    Nabeel A
    Moderator

    Hi again,

    Would you mind providing us with login credentials so we can take a closer look? To do this, you can make a post with the following info:

    – Link to your site
    – WordPress Admin username / password
    – FTP credentials

    Don’t forget to select Set as private reply. This ensures your information is only visible to our staff.

    #1390454

    ingonuts
    Participant
    This reply has been marked as private.
    #1391072

    Rad
    Moderator

    Hi there,

    The code was incomplete, please add this instead

      function x_ethos_entry_meta() {
    
        //
        // Author.
        //
    
        $author = sprintf( ' %1$s %2$s</span>',
          __( 'by', '__x__' ),
          get_the_author()
        );
    
        //
        // 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>%1$s %2$s',
              __( 'In', '__x__' ),
              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>%1$s %2$s',
            __( 'In', '__x__' ),
            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' : 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.
        //
        ob_start(); post_read_time();
        $post_read_time = ob_get_clean();
    
        if ( !empty( $post_read_time )) {
        	$post_read_time .= ', ';
        }
    
        if ( x_does_not_need_entry_meta() ) {
          return;
        } else {
          printf( '<p class="p-meta">%1$s%2$s%3$s%4$s</p>',
            $date,
            $post_read_time,
            $author,
            $comments
          );
        }
    
      }

    Cheers!

    #1397975

    ingonuts
    Participant

    Any chance you could take a look, everything is completely broken now.

    #1398318

    ingonuts
    Participant

    Hello, the code you gave me completely broke the formatting of my site when I apply it. Please advise.

    #1398720

    Paul R
    Moderator

    Hi,

    I went ahead and added this code in your child theme’s functions.php file

    
    function x_ethos_entry_meta() {
    
        //
        // Author.
        //
    
        $author = sprintf( ' %1$s %2$s</span>',
          __( 'by', '__x__' ),
          get_the_author()
        );
    
        //
        // 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>%1$s %2$s',
              __( 'In', '__x__' ),
              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>%1$s %2$s',
            __( 'In', '__x__' ),
            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' : 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 = '';
    
        }
    
         $post_read_time = do_shortcode('[rt_reading_time label="Reading Time:" postfix="minutes"]');
    
        //
        // Output.
        //
    
        if ( x_does_not_need_entry_meta() ) {
          return;
        } else {
          printf( '<p class="p-meta">%1$s%2$s%3$s%4$s</p>',
            $date,
            $post_read_time,
            $author,        
            $comments
          );
        }
    
      }
    

    I did use the shortcode [rt_reading_time label="Reading Time:" postfix="minutes"]

    You may change the label and time postfix.

    Thanks