[Support request] How to add “Published on” and “Updated on” without CSS?

Home Forums Support [Support request] How to add “Published on” and “Updated on” without CSS?

Home Forums Support How to add “Published on” and “Updated on” without CSS?

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2244797
    NrH

    Hello, I have two questions, but the most important one is how can I add the two labels “Published on” and “Updated on” before the Posted on and Updated on date times in the blogposts and archives without using pseudoelements and CSS?

    And the second question is how can I show the Updated on date time on the blogposts/archives instead of the “Published on” date time? Thank you in advance!

    #2244862
    David
    Staff
    Customer Support

    Hi there,

    are you wanting to display both the Updated on and Publish on dates ? Or just one of them ?

    And are you a GenerateBlocks user ?

    #2244926
    NrH

    Hi! Not a GenerateBlocks user, still using sections

    I want to display the Updated on-date if it exists, and if not Published on, but I also want to display a text before these dates that says “Published on …” ahead of the published on date, and “Updated on” ahead of the updated on date, without using CSS if that’d be possible?

    Thank you again!

    #2245002
    David
    Staff
    Customer Support

    Try this PHP Snippet:

    add_filter( 'generate_post_date_output', function( $output, $time_string ) {
        $time_string = '<span class="date-label">Published on: </span><time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
    
        // Check modified date is newer thant published
    
        $updated_time = get_the_modified_time( 'U' );
        $published_time = get_the_time( 'U' ) + 86400;
    
        // If modified date exists then ouput both time strings with modified date
        
        if ( $updated_time > $published_time ) {
            $time_string = '<span class="date-label">Updated on: </span><time class="entry-date updated-date" 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() )
        );
    
        return sprintf( '<span class="posted-on">%s</span> ',
            $time_string
        );
    }, 10, 2 );
    #2245021
    NrH

    Awesome works!! Thank you so much!

    #2245239
    David
    Staff
    Customer Support

    Glad to hear that!

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