[Resolved] Adding updated date on homepage

Home Forums Support [Resolved] Adding updated date on homepage

Home Forums Support Adding updated date on homepage

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #924556
    Garrick

    Hi,

    I’m trying to add an “updated on” below the title in the homepage but so far I’m having trouble. On the posts, the “updated on:” date appears without any issues.

    This is the code I’m using

    if ( ! function_exists( 'generate_posted_on' ) ) :
    /**
     * Prints HTML with meta information for the current post-date/time and author.
     */
    function generate_posted_on() {
    
    	if ( 'post' !== get_post_type() ) 
    		return;
    	
    	$date = apply_filters( 'generate_post_date', true );
    	$author = apply_filters( 'generate_post_author', true );
    		
    	$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on %2$s</time>';
    	if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) )
    		$time_string .= '<time class="updated" datetime="%3$s" itemprop="dateModified">Updated on %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() )
    	);
    	
    	// If our date is enabled, show it
    	if ( $date ) :
    		printf( '<span class="posted-on">%1$s</span>',
    			sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
    				esc_url( get_permalink() ),
    				esc_attr( get_the_time() ),
    				$time_string
    			)
    		);
    	endif;
    	
    	// If our author is enabled, show it
    	if ( $author ) :
    		printf( ' <span class="byline">%1$s</span>',
    			sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%1$s <a class="url fn n" href="%2$s" title="%3$s" rel="author" itemprop="url"><span class="author-name" itemprop="name">%4$s</span></a></span>',
    				__( 'by ','generate'),
    				esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    				esc_attr( sprintf( __( 'View all posts by %s', 'generate' ), get_the_author() ) ),
    				esc_html( get_the_author() )
    			)
    		);
    	endif;
    	
    }
    endif;
    
    /* Add Last update below titles, for pages */
    if ( ! function_exists( 'generate_post_meta' ) ) :
    /**
     * Build the post meta
     *
     * @since 1.3.29
     */
    add_action( 'generate_after_entry_title', 'generate_post_meta' );
    function generate_post_meta()
    {
    	if ( 'post' == get_post_type() || 'page' == get_post_type() ) : ?>
    		<div class="entry-meta">
    			<?php generate_posted_on(); ?>
    		</div><!-- .entry-meta -->
    	<?php endif;
    }
    endif;

    Can you please help me.

    #924692
    David
    Staff
    Customer Support

    Hi there,

    the last piece of you code is hooking into the generate_after_entry_title which doesn’t exist on the Page template. Try the generate_after_entry_header instead.

    Visual hook guide is a handy reference:

    https://docs.generatepress.com/article/hooks-visual-guide/#static-page

    #925275
    Garrick

    Hi David,

    I tried making the change but I still don’t see the “updated on” in the homepage.

    I’ve placed the code in the child theme under functions.php.

    #925302
    David
    Staff
    Customer Support

    So this part of the code is limiting the output to posts:

    if ( 'post' !== get_post_type() )

    try:

    if ( 'post' !== get_post_type() || 'page' !== get_post_type() )

    #925368
    Garrick

    Hi I made the edits in the code but strangely, the updated on disappears on both the page and post

    #926081
    Tom
    Lead Developer
    Lead Developer

    What if you do this instead?:

    add_filter( 'generate_entry_meta_post_types', function( $type ) {
        $types[] = 'page';
    
        return $types;
    } );
    #926465
    Garrick

    It doesn’t work.

    Here’s what I’ve tried so far…

    I tried this

    if ( 'post' !== get_post_type() || 'page' !== get_post_type() )

    but the updated date doesn’t show up. Then I tried this

    if ( 'post' !== get_post_type() ||)

    And it shows up on the posts page. Then I tried this

    if ( 'page' !== get_post_type() ||)

    And it shows up in the pages including the homepage.

    #927166
    Tom
    Lead Developer
    Lead Developer

    And my code didn’t work at all when you removed all of your other changes?

    #927615
    Garrick

    Unfortunately it did not. To clarify, I’d like to add the updated date on BOTH pages and posts. Since my homepage is a long form post, I have to add an updated date to it.

    This is the whole code in the functions.php on the child theme.

    <?php
    /**
     * GeneratePress child theme functions and definitions.
     *
     * Add your custom PHP in this file. 
     * Only edit this file if you have direct access to it on your server (to fix errors if they happen).
     */
    
    function generatepress_child_enqueue_scripts() {
    	if ( is_rtl() ) {
    		wp_enqueue_style( 'generatepress-rtl', trailingslashit( get_template_directory_uri() ) . 'rtl.css' );
    	}
    }
    add_action( 'wp_enqueue_scripts', 'generatepress_child_enqueue_scripts', 100 );
    
    if ( ! function_exists( 'generate_posted_on' ) ) :
    /**
     * Prints HTML with meta information for the current post-date/time and author.
     */
    function generate_posted_on() {
    
    	if ( 'post' !== get_post_type() )
    		return;
    	
    	$date = apply_filters( 'generate_post_date', true );
    	$author = apply_filters( 'generate_post_author', true );
    		
    	if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) )
    		$time_string = 'Updated on <time class="updated" datetime="%3$s" itemprop="dateModified">%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() )
    	);
    	
    	// If our date is enabled, show it
    	if ( $date ) :
    		printf( '<span class="posted-on">%1$s</span>',
    			sprintf( '%3$s',
    				esc_url( get_permalink() ),
    				esc_attr( get_the_time() ),
    				$time_string
    			)
    		);
    	endif;
    	
    	// If our author is enabled, show it
    	if ( $author ) :
    		printf( ' <span class="byline">%1$s</span>',
    			sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%1$s <a class="url fn n" href="%2$s" title="%3$s" rel="author" itemprop="url"><span class="author-name" itemprop="name">%4$s</span></a></span>',
    				__( 'by ','generate'),
    				esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    				esc_attr( sprintf( __( 'View all posts by %s', 'generate' ), get_the_author() ) ),
    				esc_html( get_the_author() )
    			)
    		);
    	endif;
    	
    }
    endif;
    
    /* Removing datePublished from comment section */
    if ( ! function_exists( 'generate_comment' ) ) {
    	/**
    	 * Template for comments and pingbacks.
    	 *
    	 * Used as a callback by wp_list_comments() for displaying the comments.
    	 */
    	function generate_comment( $comment, $args, $depth ) {
    		$args['avatar_size'] = apply_filters( 'generate_comment_avatar_size', 50 );
    		if ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) : ?>
    
    		<li id="comment-<?php comment_ID(); ?>" <?php comment_class(); ?>>
    			<div class="comment-body">
    				<?php _e( 'Pingback:', 'generatepress' ); // WPCS: XSS OK. ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( 'Edit', 'generatepress' ), '<span class="edit-link">', '</span>' ); ?>
    			</div>
    
    		<?php else : ?>
    
    		<li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
    			<article id="div-comment-<?php comment_ID(); ?>" class="comment-body" itemscope itemtype="https://schema.org/Comment">
    				<footer class="comment-meta">
    					<?php
    					if ( 0 != $args['avatar_size'] ) {
    						echo get_avatar( $comment, $args['avatar_size'] );
    					}
    					?>
    					<div class="comment-author-info">
    						<div class="comment-author vcard" itemprop="author" itemscope itemtype="https://schema.org/Person">
    							<?php printf( '<cite itemprop="name" class="fn">%s</cite>', get_comment_author() ); ?>
    						</div><!-- .comment-author -->
    
    						<div class="entry-meta comment-metadata">
    								<time datetime="<?php printf( // WPCS: XSS OK.
    										/* translators: 1: date, 2: time */
    										_x( '%1$s', '1: date, 2: time', 'generatepress' ),
    										get_comment_date()
    									); ?>">
    									<?php printf( // WPCS: XSS OK.
    										/* translators: 1: date, 2: time */
    										_x( '%1$s', '1: date, 2: time', 'generatepress' ),
    										get_comment_date()
    									); ?>
    								</time>
    							<?php edit_comment_link( __( 'Edit', 'generatepress' ), '<span class="edit-link">| ', '</span>' ); ?>
    							<?php
    							comment_reply_link( array_merge( $args, array(
    								'add_below' => 'div-comment',
    								'depth'     => $depth,
    								'max_depth' => $args['max_depth'],
    								'before'    => '<span class="reply">| ',
    								'after'     => '</span>',
    							) ) );
    							?>
    						</div><!-- .comment-metadata -->
    					</div><!-- .comment-author-info -->
    
    					<?php if ( '0' == $comment->comment_approved ) : ?>
    						<p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'generatepress' ); // WPCS: XSS OK. ?></p>
    					<?php endif; ?>
    				</footer><!-- .comment-meta -->
    
    				<div class="comment-content" itemprop="text">
    					<?php comment_text(); ?>
    				</div><!-- .comment-content -->
    			</article><!-- .comment-body -->
    		<?php
    		endif;
    	}
    }
    #928601
    Tom
    Lead Developer
    Lead Developer

    Can you remove the generate_posted_on() function from your child theme, then try the filter I posted? You shouldn’t need to overwrite the entire generate_posted_on() function.

    #939316
    Garrick

    Hi Tom,

    I tried removing it but it didn’t work.

    #939719
    Tom
    Lead Developer
    Lead Developer

    Can you try this?:

    add_filter( 'generate_entry_meta_post_types', function( $types ) {
        $types[] = 'page';
    
        return $types;
    } );
    
    add_action( 'wp', function() {
        if ( is_page() ) {
            add_action( 'generate_after_entry_header', 'generate_post_meta' );
        }
    } );
    #1008469
    Garrick

    Hi Tom,

    This code actually worked for the homepage and posts. How can I add the other details such as “updated on:”, etc, etc. like the function I posted earlier.

    #1008772
    Tom
    Lead Developer
    Lead Developer
    #1322906
    Garrick

    Here’s the final code that solved the issue. Now about to display the dateModified on the homepage.

    add_filter( 'generate_post_date_output', 'tu_show_modified_date' );
    function tu_show_modified_date() {
    	$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time> ';
    	
    	if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
    		$time_string = '<time class="updated" datetime="%3$s" itemprop="dateModified">%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() )
    	);
    
    	printf( 
    		'<span class="posted-on">%1$s</span>', // WPCS: XSS ok, sanitization ok.
    		sprintf( 'Updated on <a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
    			esc_url( get_permalink() ),
    			esc_attr( get_the_time() ),
    			$time_string
    		)
    	);
    }
Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.