• Resolved Bri

    (@tinnyfusion)


    Good Afternoon,

    I wonder if someone is able to assist with a CSS issue I have please?

    I am currently working on localhost only and even though I am using LocalWP v8.3.2+6660 to develop, it seems that it will not let me share a live link (GraphQL error).

    Anyway, I am using a couple of snippets to get min reading time and to also show published date of post and Last Updated date of post (not one or the other). These are both working just fine. The issue I am having is that when a post has been updated it (correctly shows) both the original published date as well as the last updated date which is what I want. However, I am struggling with the CSS to align these next to each other.

    See below:

    I would like the updated section to be to the right of the published part with a 1px grey line separating the two (EG: color: 333; border-right: 1px) as opposed to underneath it. I am thinking maybe display: inline-block; but I have hit a wall with this…

    Here is the code I currently have:

    // Calclate estimated reading time of Article/Post from number of words
    function reading_time() {
        global $post;
        $content = get_post_field( 'post_content', $post->ID );
        $word_count = str_word_count( strip_tags( $content ) );
        $readingtime = ceil($word_count / 183); // Change to edit reading speed (WPM). Slow=100, Ave=183, and fast=260
    
        if ($readingtime == 1) {
          $timer = " min"; // minute
        } else {
          $timer = " mins"; // minutes
        }
        $totalreadingtime = $readingtime . $timer;
    
        return $totalreadingtime;
    }
    
    // Show both Published date and Last Updated date for Posts/Articles
    add_filter( 'generate_post_date_output', function( $output, $time_string ) {
        $time_string = '<span class="label">Published: </span><time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
    
        if ( get_the_date() !== get_the_modified_date() ) {
            $time_string = '<span class="label">Published: </span><time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time><span class="label">Updated: </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( 'jS F, Y' ) ), // Adjusted format for published date
            esc_attr( get_the_modified_date( 'c' ) ),
            esc_html( get_the_modified_date( 'jS F, Y' ) ) // Adjusted format for modified date
        );
    
        return sprintf( '<div class="posted-on">%s</div> ', $time_string );
    }, 10, 2 );
    
    
    // Generate author image and name with reading time
    add_filter( 'generate_post_author_output', function() {
        // Calculate reading time dynamically
        $reading_time = reading_time();
    
        // Output author information without the link
        return sprintf( '<div class="author vcard">%s</div><div class="author-wrap"><span>Written by <span class="author-name" itemprop="name">%s&nbsp;</span></span><span class="label" id="reading-time">Read Time: %s</span></div>',
                get_avatar( get_the_author_meta( 'ID' ) ),
                esc_html( get_the_author() ),
                $reading_time
        );
    } );
    
    add_filter( 'generate_header_entry_meta_items', function() {
        return array(
            'author',
            'date',
        );
    } );

    If you require any further information please let me know.

    Thank you.

Viewing 15 replies - 1 through 15 (of 36 total)
  • Hi there,

    it would be easier to control with some additional HTML. Your second PHP snippet where the 2 x dates are returned , change it to:

    // Show both Published date and Last Updated date for Posts/Articles
    add_filter( 'generate_post_date_output', function( $output, $time_string ) {
        $time_string = '<div><span class="label">Published: </span><time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time></div>';
    
        if ( get_the_date() !== get_the_modified_date() ) {
            $time_string .= '<div><span class="label">Updated: </span><time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">%4$s</time></div>';
        }
        $time_string = sprintf( $time_string,
            esc_attr( get_the_date( 'c' ) ),
            esc_html( get_the_date( 'jS F, Y' ) ), // Adjusted format for published date
            esc_attr( get_the_modified_date( 'c' ) ),
            esc_html( get_the_modified_date( 'jS F, Y' ) ) // Adjusted format for modified date
        );
    
        return sprintf( '<div class="posted-on">%s</div> ', $time_string );
    }, 10, 2 );

    This will add a <div> around each date with its label.

    Then use this CSS to create the layout and add the border:

    .posted-on,
    .posted-on > div {
        display: flex;
    }
    .posted-on > div {
        flex-direction: column;
    }
    .posted-on > div:first-child {
        padding-right: 10px;
        margin-right: 10px;
        border-right: 1px solid #ccc;
    }
    Thread Starter Bri

    (@tinnyfusion)

    Thank you. I have implemented the changes you gave but given how the snippet works I won’t know if it has worked until a day has passed… Unless that is, you are also able to advise on an alteration to allow it to recognise edits during the same day (going on time too)?

    Thread Starter Bri

    (@tinnyfusion)

    I wasn’t asking, more joking and I realise this is out of scope for free GeneratePress. I will check tomorrow and let you know.

    Thank you again.

    Unless you have some caching or some other function that intereres with those snippets executing you should see the changes immediately.

    Thread Starter Bri

    (@tinnyfusion)

    No caching other than the browser (which has been cleared) and a shift+F5. Still only shows the published date.

    • This reply was modified 7 months, 3 weeks ago by Bri.
    Thread Starter Bri

    (@tinnyfusion)

    The site is now available to view: crikeythatsmint.com

    One thing, (once confirmed working),if viewing on mobile then the current CSS/design will not be able to show both the published and Last Modified date.

    How would I make it show as follows on mobile?:

    Stacked vertically and cantered under the users name.
    Published: date here
    Udpated: date here

    The site is behind a maintenance mode.

    Thread Starter Bri

    (@tinnyfusion)

    The other side showing authors name and read time has a smaller gap now the extra div has been added to the published and modified date section.

    Can this be corrected?

    *Maint mode now disabled

    • This reply was modified 7 months, 3 weeks ago by Bri.
    • This reply was modified 7 months, 3 weeks ago by Bri.
    Thread Starter Bri

    (@tinnyfusion)

    Additionally, I am not sure if you have seen my style.css but it has both your latest CSS (below) and the CSS I already had (also below). Is this correct?

    /* Show post/article published date and last updated date inline START */
    .posted-on,
    .posted-on > div {
        display: flex;
    }
    .posted-on > div {
        flex-direction: column;
    }
    .posted-on > div:first-child {
        padding-right: 10px;
        margin-right: 10px;
        border-right: 1px solid #ccc;
    }
    /* Show post/article published date and last updated date inline END */
    /* User meta information START */
    .entry-meta:not(footer),
    .entry-meta .posted-on,
    .entry-meta .author-wrap {
        display: flex;
    }
    
    .entry-meta {
        align-items: center;
        justify-content: center;
    }
    
    .entry-meta .posted-on,
    .entry-meta .author-wrap {
        flex-direction: column;
        font-size: 16px;
        padding: 0 25px;
        flex: 1;
    }
    
    .entry-meta .posted-on {
        text-align: right;
    }
    
    .entry-meta .label {
        font-size: 14px;
        color: #aaa;
        margin-bottom: 0.25em;
    }
    
    .author img {
        width: 60px;
        height: 60px;
    	-webkit-border-radius: 50%;
    	-moz-border-radius: 50%;
        border-radius: 50%;
        vertical-align: middle;
    }
    /* User meta information END */

    For the mobile layout:

    @media(max-width: 768px) {
        .entry-header .entry-meta {
            flex-wrap: wrap;
        }
        .entry-header .entry-meta .posted-on {
            flex-basis: 100%;
        }
        .entry-header .entry-meta .posted-on > div {
            flex-direction: row;
            column-gap: 10px;
            align-items: flex-start;
        }
    }
    Thread Starter Bri

    (@tinnyfusion)

    Thank you David,

    The problem is though is that I cannot test it as it is still not showing the modified date… Maybe there is something wrong with the snippet? I found it on another GeneratePress related post in these forums regarding the same question that I found while searching for answers before posting myself.

    Also, any chance you could there is a fix for this?

    To clarify, I currently have the following CSS:

    /* Show post/article published date and last updated date inline START */
    .posted-on,
    .posted-on > div {
        display: flex;
    }
    .posted-on > div {
        flex-direction: column;
    }
    .posted-on > div:first-child {
        padding-right: 10px;
        margin-right: 10px;
        border-right: 1px solid #ccc;
    }
    
    @media(max-width: 768px) {
        .entry-header .entry-meta {
            flex-wrap: wrap;
        }
        .entry-header .entry-meta .posted-on {
            flex-basis: 100%;
        }
        .entry-header .entry-meta .posted-on > div {
            flex-direction: row;
            column-gap: 10px;
            align-items: flex-start;
        }
    }
    /* Show post/article published date and last updated date inline END */
    
    /* User meta information START */
    .entry-meta:not(footer),
    .entry-meta .posted-on,
    .entry-meta .author-wrap {
        display: flex;
    }
    
    .entry-meta {
        align-items: center;
        justify-content: center;
    }
    
    .entry-meta .posted-on,
    .entry-meta .author-wrap {
        flex-direction: column;
        font-size: 16px;
        padding: 0 25px;
        flex: 1;
    }
    
    .entry-meta .posted-on {
        text-align: right;
    }
    
    .entry-meta .label {
        font-size: 14px;
        color: #aaa;
        margin-bottom: 0.25em;
    }
    
    .author img {
        width: 60px;
        height: 60px;
    	-webkit-border-radius: 50%;
    	-moz-border-radius: 50%;
        border-radius: 50%;
        vertical-align: middle;
    }
    /* User meta information END */
    • This reply was modified 7 months, 3 weeks ago by Bri.

    has the post been modified ?
    As the condition in the function is :

    if ( get_the_date() !== get_the_modified_date() ) {

    which will return the modified date if its not the same as the published ( get_the_date )

    Thread Starter Bri

    (@tinnyfusion)

    Not changed as far I know. This is what I currently have (along with the CSS I posted earlier):

    // Calclate estimated reading time of Article/Post from number of words
    function reading_time() {
        global $post;
        $content = get_post_field( 'post_content', $post->ID );
        $word_count = str_word_count( strip_tags( $content ) );
        $readingtime = ceil($word_count / 183); // Change to edit reading speed (WPM). Slow=100, Ave=183, and fast=260
    
        if ($readingtime == 1) {
          $timer = " min"; // minute
        } else {
          $timer = " mins"; // minutes
        }
        $totalreadingtime = esc_html($readingtime . $timer);
    
        return $totalreadingtime;
    }
    
    // Show both Published date and Last Updated date for Posts/Articles
    add_filter( 'generate_post_date_output', function( $output, $time_string ) {
        $time_string = '<div><span class="label">Published: </span><time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time></div>';
    
        if ( get_the_date() !== get_the_modified_date() ) {
            $time_string .= '<div><span class="label">Updated: </span><time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">%4$s</time></div>';
        }
        $time_string = sprintf( $time_string,
            esc_attr( get_the_date( 'c' ) ),
            esc_html( get_the_date( 'jS F, Y' ) ), // Adjusted format for published date
            esc_attr( get_the_modified_date( 'c' ) ),
            esc_html( get_the_modified_date( 'jS F, Y' ) ) // Adjusted format for modified date
        );
    
        return sprintf( '<div class="posted-on">%s</div> ', $time_string );
    }, 10, 2 );
    
    
    // Generate author image and name with reading time
    add_filter( 'generate_post_author_output', function() {
        // Calculate reading time dynamically
        $reading_time = reading_time();
    
        // Output author information without the link
        return sprintf( '<div class="author vcard">%s</div><div class="author-wrap"><span>Written by <span class="author-name" itemprop="name">%s&nbsp;</span></span><span class="label" id="reading-time">Read Time: %s</span></div>',
                get_avatar( get_the_author_meta( 'ID' ) ),
                esc_html( get_the_author() ),
                $reading_time
        );
    } );
    
    add_filter( 'generate_header_entry_meta_items', function() {
        return array(
            'author',
            'date',
        );
    } );

    Not sure if it’d be easier to combine the above three snippets in to one..

    Thread Starter Bri

    (@tinnyfusion)

    It would be nice to incorporate human_time_diff but I haven’t found a way to alter the modified date using that..

    So far I have only been able to target/change get_the_date, the_date, get_the_time, and the_time.

    I would have thought it would be get_the_modified_date, the_modified_date, get_the_modified_time, and the_modified_time but these do not seem to do anything.

    Thread Starter Bri

    (@tinnyfusion)

    I have been looking at the Published and Last Updated snippet and it seems the version I have on the live site does not match the one I have on my local machine.

    This is the correct one (which has also been added to the live site to replace the old one). Might be easier to work with now… Sorry about that.

    // Show both Published date and Last Updated date for Posts/Articles
    add_filter( 'generate_post_date_output', function( $output, $time_string ) {
        $time_string = '<span class="label">Published</span><time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
    
        if ( get_the_date() !== get_the_modified_date() ) {
    		$time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified"><span class="label">Updated:</span>%4$s</time>';
            $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished"><span class="label">Published:</span><br />%2$s</time> <span style="color: #000;">|</span> <time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified"><span class="label">Last Updated:</span><br />%4$s</time>';
        }
    
        $time_string = sprintf( $time_string,
            esc_attr( get_the_date( 'c' ) ),
            esc_html( get_the_date( 'jS F, Y' ) ), // Adjusted format for published date
            esc_attr( get_the_modified_date( 'c' ) ),
            esc_html( get_the_modified_date( 'jS F, Y' ) ) // Adjusted format for modified date
        );
    
        return sprintf( '<div class="posted-on">%s</div> ', $time_string );
    }, 10, 2 );

    Now showing like this on desktop:

    Tablet:

    And mobile:

    To confirm (and if possible), to have the published date moved to the side of last updated date (so everything is in line and add a grey line between them for separation/clarity.

    On mobile view, show published date as normal, but if post has been updated, hide published date and only show last updated date in it’s place.

    I realise that you may have to add different classes to either published/updated dates. If so, just advise what to change them to so that they correctly pointing to the correct CSS within style.css.

    Thank you

    • This reply was modified 7 months, 3 weeks ago by Bri.
Viewing 15 replies - 1 through 15 (of 36 total)
  • The topic ‘CSS Alignment Issue’ is closed to new replies.