[Support request] Display Date Field from ACF

Home Forums Support [Support request] Display Date Field from ACF

Home Forums Support Display Date Field from ACF

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1671366
    Jim

    I use a custom date field created in Advanced Custom Fields to display an updated date on my pages. I don’t like using the modified date because I don’t want the on-page date to change every time a fix a typo; I only want the date to change when I explicitly set it.

    I’m trying to figure out how to use this date with GP. In my previous theme, I had this working in a custom single template:

    <?php if ( get_field( 'date_updated' ) ): 
      echo 'Updated ' . get_field( 'date_updated' );
     else: 
      the_date();
    endif; ?> 

    How might I adapt this for use in GP?

    To further complicate matters, I’m also trying to combine it with these snippets.

    This one to display the author before the date:

    add_filter( 'generate_header_entry_meta_items', function() {
        return array(
            'author',
            'date',
           
        );
    } );

    And this one to insert a pipe character between them:

    add_filter( 'generate_inside_post_meta_item_output', function( $output, $item ) {
    	if ( 'tags' === $item || 'date' === $item ) {
            return ' | ';
        }
        return $output;
    }, 50, 2 );

    Any suggestions on how to implement all of this most efficiently?

    #1671424
    Elvin
    Staff
    Customer Support

    You can use the generate_post_date_output filter for this.

    Example:

    add_filter( 'generate_post_date_output', function() {
        if( ! is_singular( array('page', 'attachment', 'post') ) ){
    
            $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">Updated On %4$s</time> ' . $time_string;
            }
    
            $time_string = sprintf( $time_string,
                esc_attr( get_the_date( 'c' ) ),
                esc_html( get_the_date('Y') ),
                esc_attr( get_the_modified_date( 'c' ) ),
                get_field( 'date_updated' )
            );
    
            return sprintf( '<span class="posted-on">%1$s</span>', // WPCS: XSS ok, sanitization ok.
                sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
                    esc_url( get_permalink() ),
                    esc_attr( get_the_time() ),
                    $time_string
                )
            );
        }
    
    });

    We basically changed the modified date source from get_the_modified_date() to your custom field.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.