CMS Development

jw20
Membre

Updated Date on Blog

Résolue

I am editing our blog template to include an "Updated on" date. I have followed this article (which has the code to copy missing) and it gets close, but it doesn't completely work. It works when there's an updated date, but when you first publish the post, it displays "Published on February 3, 2021, updated on February 3, 2021." I want it to only show the "updated on" if the post is updated. The code from HubSpot is below. I don't think I need the UTC offset because I'm not showing a time, but I've included it.

 

{% set update_time = content.updated %}
{% set offset = 14400000 %}
{% set new_time = update_time - offset %}
Published on {{ content.publish_date|datetimeformat('%B %e, %Y') }}
{% if content.updated|unixtimestamp > content.publish_date|unixtimestamp %}
    , updated on {{ new_time|datetimeformat('%B %e, %Y') }}
{% endif %}

 

I have also tried this - with the same result (updated date shows regardless if there was an update).

 

Published on {{ content.publish_date|datetimeformat('%B %e, %Y') }}
 {% if content.updated|unixtimestamp > content.publish_date|unixtimestamp %}
   , updated on {{ content.updated|datetimeformat('%B %e, %Y') }}
{% endif %}

 

Can anyone help with the syntax for the "if" so that the "updated on" only shows if the post is updated? I usually work in php and am just learning Hubl.

0 Votes
2 Solutions acceptées
dennisedson
Solution
Équipe de développement de HubSpot
Équipe de développement de HubSpot

Updated Date on Blog

Résolue

By some coincedence, this was also discussed on the dev slack here

Here is the code that finally worked (update with your copy)

{% if content.updated|unixtimestamp//1000 > content.publish_date|unixtimestamp//1000 %}
  Originally posted on {{ content.publish_date_localized }}
  <br>Last updated on {{ content.updated_date_local_time|datetimeformat('%B %e, %Y') }}
{% else %}
  {{ content.publish_date_localized }}
{% endif %}

Voir la solution dans l'envoi d'origine

jw20
Solution
Membre

Updated Date on Blog

Résolue

I changed the updated date from 

{{ content.updated_date_local_time|datetimeformat('%B %e, %Y') }}

to

{{ content.updated|datetimeformat('%B %e, %Y') }}

Otherwise it showed the following day.

Voir la solution dans l'envoi d'origine

19 Réponses
KeyWestScott
Conseiller clé

Updated Date on Blog

Résolue

My word.  Why in the world isn't Hubspot simply making this an option?  " Click here to show date of article update. "

 

All of Hubspot's native articles show an "updated" date.  Come on guys.....

 

Scott.

MLadenburg
Contributeur | Partenaire solutions
Contributeur | Partenaire solutions

Updated Date on Blog

Résolue

@dennisedson  and @jw20 ,

 

Thank you for all of your help! I tried @dennisedson 's code, but was really wanting the "posted on" and "updated on" written out - so was eventually able to get our dev's eyeballs on it to help me out. I'm posting it here and a screenshot with how it looks in case anyone ever needs it!

 

{% if content.updated|unixtimestamp//1000 > content.publish_date|unixtimestamp//1000 %}
          Updated on {{ content.updated_date_local_time|datetimeformat('%B %e, %Y') }}
            <br>Posted on {{ content.publish_date_localized }} by 
            {% if content.blog_post_author %}
              <a class="author-link" href="{{ group.absolute_url }}/author/{{ content.blog_post_author.slug }}">{{ content.blog_post_author.display_name }}</a>
            {% endif %}
          {% else %}
              Posted on {{ content.publish_date_localized }} by {% if content.blog_post_author %} <a class="author-link" href="{{ group.absolute_url }}/author/{{ content.blog_post_author.slug }}">{{ content.blog_post_author.display_name }}</a>
              {% endif %}
          {% endif %}

 

Screen Shot 2021-07-07 at 2.44.33 PM.png

 

jw20
Membre

Updated Date on Blog

Résolue

@dennisedson 

I remember that if I updated the post at 4pm it would display as the next day. I don't have time right now to dig through my notes, but neither of these worked for the "Updated" date - (I'm EST) if updated after 4pm -

{{ content.publish_date_localized|datetimeformat('%B %e, %Y') }}

 

{{ content.updated_date_local_time|datetimeformat('%B %e, %Y') }}

 

I ended up using this below, but the client decided not to include the words "Posted on"

{{ content.updated|datetimeformat('%B %e, %Y') }} 

  

I'm sure it was some syntax issue on my end, but it was necessary to test across times and scenarios (like, saving a draft vs published) to double check it worked.

jw20
Solution
Membre

Updated Date on Blog

Résolue

I changed the updated date from 

{{ content.updated_date_local_time|datetimeformat('%B %e, %Y') }}

to

{{ content.updated|datetimeformat('%B %e, %Y') }}

Otherwise it showed the following day.

MLadenburg
Contributeur | Partenaire solutions
Contributeur | Partenaire solutions

Updated Date on Blog

Résolue

@jw20  and @dennisedson ,

 

Thank you for your super helpful post. I'm trying to get mine to read "posted on," unless it's been updated so that it says, "updated on" - so basically overriding the "posted on."

Below is my code (largely copied and pasted from your efforts). At the moment, it posts both "posted on" and "updated on" - do you know what function would clear the original "posted on."? Thank you for any help.

 

<span class="hs-author-label">Posted on <a href="{{content.absolute_url}}">{{ content.publish_date_localized }}</a> by
                    {%if content.blog_post_author %}</span><br>
              {%if content.updated|unixtimestamp//1000 > content.publish_date|unixtimestamp//1000 %}
  <br>Updated on {{ content.updated|datetimeformat('%B %e, %Y') }}
{% else %}
  {{ content.publish_date_localized }}
{% endif %}
0 Votes
dennisedson
Équipe de développement de HubSpot
Équipe de développement de HubSpot

Updated Date on Blog

Résolue

Hey @MLadenburg 

Try this:

{% if content.updated|unixtimestamp//1000 > content.publish_date|unixtimestamp//1000 %}
  Updated on {{ content.updated_date_local_time|datetimeformat('%B %e, %Y') }}

{% else %}
Posted on  {{ content.publish_date_localized|datetimeformat('%B %e, %Y') }}
{% endif %}

@jw20 , what happened with yours?  I just tested and this works 🤔

0 Votes
jw20
Membre

Updated Date on Blog

Résolue

@MLadenburg  -  Unfortunately, I'm not any help. We ultimately didn't add "Posted on" -  although I don't believe that I had gotten that to work either.  The blog post displays the date and when updated, posts "Updated on" and the new date.  

MLadenburg
Contributeur | Partenaire solutions
Contributeur | Partenaire solutions

Updated Date on Blog

Résolue

@jw20  - Thanks for responding. Would you be willing to post the code in total? I'd love to get ours to do exactly that for the time being.

0 Votes
jw20
Membre

Updated Date on Blog

Résolue

I updated how I had the time set up and it's working perfectly.

Thanks for the assistance!

dennisedson
Équipe de développement de HubSpot
Équipe de développement de HubSpot

Updated Date on Blog

Résolue

hey @jw20

Mind adding your final code here and accepting it as the solution 🙏

0 Votes
jw20
Membre

Updated Date on Blog

Résolue

Here is what I have:

{% if content.updated|unixtimestamp//1000 > content.publish_date|unixtimestamp//1000 %} 
  Originally posted on {{ content.publish_date_local_time|datetimeformat('%B %e, %Y')}} <br>
  Last updated on {{ content.updated_date_local_time|datetimeformat('%B %e, %Y') }} 
{% else %} 
  Published on {{ content.publish_date_local_time|datetimeformat('%B %e, %Y') }} 
{% endif %} 

Of my four posts, they work - aside from one: it was updated yesterday at 4pm Eastern time, but is showing the update date as today.

 

Does something else need added as far as timezone? Something in the first line?

 

Thank you so much for the help!

 

0 Votes
jw20
Membre

Updated Date on Blog

Résolue

That's a little big closer, @dennisedson  -

When I publish a new post it says "Updated on Feb 5" - except it should say "Published on Feb 5"

The old posts that have been updated are correct and show the update date.

 

Does changing the content.created to content.published make a difference? Is content.created a draft?

 

Thanks,

Julie

0 Votes
dennisedson
Solution
Équipe de développement de HubSpot
Équipe de développement de HubSpot

Updated Date on Blog

Résolue

By some coincedence, this was also discussed on the dev slack here

Here is the code that finally worked (update with your copy)

{% if content.updated|unixtimestamp//1000 > content.publish_date|unixtimestamp//1000 %}
  Originally posted on {{ content.publish_date_localized }}
  <br>Last updated on {{ content.updated_date_local_time|datetimeformat('%B %e, %Y') }}
{% else %}
  {{ content.publish_date_localized }}
{% endif %}
TShewchuk
Membre

Updated Date on Blog

Résolue

blog update date.pngHello! I tried inserting the code above into our blog template- since we want to be able to show when a blog was posted, as well as if it was updated. I've been playing around with it and can't seem to find a solution where the time of orignially posted is not displayed. I just want to include date for originally posted and last updated. Any insight?

0 Votes
jw20
Membre

Updated Date on Blog

Résolue

@dennisedson - you are correct - I want it to show "Published on Feb 2" OR "Published on Feb 2, updated on Feb 3"

@piersg your version still has the "updated on" showing, even when not updated. That's the piece I need to work on.

 

I will keep trying. Thanks!

dennisedson
Équipe de développement de HubSpot
Équipe de développement de HubSpot

Updated Date on Blog

Résolue

ok @jw20 , guess I am going to have to clean up @piersg 's mess 😜

{% if content.updated|unixtimestamp > content.created|unixtimestamp %}
    Updated on {{ content.updated|datetimeformat('%B %e, %Y') }}
{% else %}
    Published on {{ content.created|datetimeformat('%B %e, %Y') }}
{% endif %}
piersg
Conseiller clé

Updated Date on Blog

Résolue

Hi @jw20 (thanks @dennisedson) and welcome! I think what you need is content.created, not content.publish_date. So:

 

Published on {{ content.created|datetimeformat('%B %e, %Y') }}
{% if content.updated|unixtimestamp > content.created|unixtimestamp %}
    , updated on {{ content.updated|datetimeformat('%B %e, %Y') }}
{% endif %}

 

dennisedson
Équipe de développement de HubSpot
Équipe de développement de HubSpot

Updated Date on Blog

Résolue

Oops!  Totally misread the question.  Somehow I read it as

If not updated -- show Published on date

else show Updated on date.

Apologies for my misleading info 😥

0 Votes
dennisedson
Équipe de développement de HubSpot
Équipe de développement de HubSpot

Updated Date on Blog

Résolue

Hey @jw20 

Welcome to the Community and congrats on getting into Hubl! 

I think your time with PHP will serve you well here 😁

What I think you need is an if else statement instead of just an if statement

Read up on it here

@piersg  is super well versed in all things Hubl and can probably lend a hand!

 

0 Votes