• Hi there!

    I have a website with Gazette theme.

    I would like to add the time of the post, next to the date, on the page of the post but not on the homepage.

    I am able to display the time instead of the date, editing the template-tag.php file, replacing “get_the_date” with “get_the_time”.

    Here is the default code of the file :

    if ( ! function_exists( 'gazette_posted_on' ) ) :
    /**
     * Prints HTML with meta information for the current post-date/time and author.
     */
    function gazette_posted_on() {
    	$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
    	if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
    		$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
    	}
    
    	$time_string = sprintf( $time_string,
    		esc_attr( get_the_date( 'c' ) ),
    		esc_html( get_the_date() ),
    		esc_attr( get_the_modified_date( 'c' ) ),
    		esc_html( get_the_modified_date() )
    	);
    
    	$posted_on = sprintf( '<a href="%1$s" rel="bookmark">%2$s</a>', esc_url( get_permalink() ), $time_string );
    
    	$byline = sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s">%2$s</a></span>', esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ), esc_html( get_the_author() ) );
    
    	if ( is_single() && ( 1 == get_theme_mod( 'gazette_author_bio' ) && get_the_author_meta( 'description' ) ) ) {
    		echo '<span class="posted-on">' . $posted_on . '</span>';
    	} else {
    		echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>';
    	}
    }
    endif;

    Two questions :
    – how can I add the time next to the date ?
    – how can I make this modification only on the posts and not the homepage ?

    Thank you!

Viewing 1 replies (of 1 total)
  • Moderator Kathryn Presner

    (@zoonini)

    The first thing you’ll need to do is make a child theme, so changes won’t be overwritten every time you update the theme. Don’t edit the original theme files. If you’re new to child themes, you can explore these guides:

    http://codex.wordpress.org/Child_Themes
    https://www.smashingmagazine.com/2016/01/create-customize-wordpress-child-theme/
    http://vimeo.com/39023468

    In your child theme’s functions file, try adding this:

    function gazette_posted_on() {
    	$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
    	if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
    		$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
    	}
    
    	$time_string = sprintf( $time_string,
    		esc_attr( get_the_date( 'c' ) ),
    		esc_html( get_the_date() ),
    		esc_attr( get_the_modified_date( 'c' ) ),
    		esc_html( get_the_modified_date() )
    	);
    
    	$time_posted = esc_html( get_the_time() );
    
    	$posted_on = sprintf( '<a href="%1$s" rel="bookmark">%2$s</a>', esc_url( get_permalink() ), $time_string );
    
    	$byline = sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s">%2$s</a></span>', esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ), esc_html( get_the_author() ) );
    
    	if ( is_single() ) {
    		echo '<span class="posted-on">' . $posted_on . '</span>';
    		echo '<span class="time-posted"> ' . $time_posted . ' </span>';
    	} elseif ( is_single() && ( 1 == get_theme_mod( 'gazette_author_bio' ) && get_the_author_meta( 'description' ) ) ) {
    		echo '<span class="posted-on">' . $posted_on . '</span>';
    	} else {
    		echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>';
    	}
    }

    This variable gets the time: $time_posted = esc_html( get_the_time() );

    While this condition displays that time on single posts:

    if ( is_single() ) {
    		echo '<span class="posted-on">' . $posted_on . '</span>';
    		echo '<span class="time-posted"> ' . $time_posted . ' </span>';
    	}

    Let me know how it goes.

Viewing 1 replies (of 1 total)
  • The topic ‘Add time of the post next to the date’ is closed to new replies.