How to Display Post URL Month And Year?

24 views
Skip to first unread message

Manu Dhiman

unread,
Apr 28, 2025, 4:53:49 PMApr 28
to Forum Blogger Ambassade Francophone
I want to extract the year and month from the post URL for a project.

e.g. https://www.example.com/05/2023/post1.html

I want to extract 05 - 2023 (Do not want to use data:post.date or data:post.lastUpdated)

I created a code, but it doesn't work very well.

Any suggestion to fix

<b:if cond='data:view.isPost'>
  <b:with value='["2021", "2022", "2023", "2024"]' var='years'>
    <b:loop values='data:years' var='year'>
      <b:with value='["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]' var='months'>
        <b:loop values='data:months' var='month'>
          <!-- Check Year -->
          <b:if cond='data:post.url contains "/" + data:year + "/"'>
            <!-- Check Month -->
            <b:if cond='data:post.url contains "/" + data:month + "/"'>
              <span>Year: <data:year /> - Month: <data:month /></span>
            </b:if>
          </b:if>
        </b:loop>
      </b:with>
    </b:loop>
  </b:with>
</b:if>

Manu Dhiman

unread,
Apr 28, 2025, 4:55:14 PMApr 28
to Forum Blogger Ambassade Francophone

Christian DEHAIS

unread,
Apr 29, 2025, 2:03:01 PMApr 29
to Forum Blogger Ambassade Francophone
Hello,

I see 2 reasons why it doesn't work :

- 1 : data:post.url doesn't exist so it cannot work, except if your code is already inside a loop that we don't see like  <b:loop values='data:posts' var='post'>. This one is easy to fix.

- 2 : the second is worse in my opinion : the condition <b:if cond='data:post.url contains "/" + data:year + "/"'> tries to compare data:post.url which is a url type to data:year which is a string type. It won't work. I have done some tests and did not get any good results.  I'm affraid that there is no solution but maybe am I wrong.

So, my suggestion is to use post.date.year and post.date.month, and as I see that you want a formatted month with 2 digits I propose you this code that gives the result you want to have :


<b:if cond='data:view.isPost'>
  <b:with value='data:posts.first' var='post'>
    <span>Year: <data:post.date.year/> - Month:
      <b:switch var='data:post.date.month'>
        <b:case value='1'/>
          01
        <b:case value='2'/>
          02
        <b:case value='3'/>
          03
        <b:case value='4'/>
          04
        <b:case value='5'/>
          05
        <b:case value='6'/>
          06
        <b:case value='7'/>
          07
        <b:case value='8'/>
          08
        <b:case value='9'/>
          09
        <b:case value='10'/>
          10
        <b:case value='11'/>
          11
        <b:case value='12'/>
          12
      </b:switch>
    </span>
  </b:with>
</b:if>

Soraya Lambrechts

unread,
Apr 29, 2025, 4:19:15 PMApr 29
to Forum Blogger Ambassade Francophone
Hi,

Your code is fine — you just need to add .raw after data:post.url.

Example: data:post.url.raw instead of data:post.url.

That should fix the problem.

Best :)

Christian DEHAIS

unread,
Apr 30, 2025, 7:49:51 AMApr 30
to Forum Blogger Ambassade Francophone
Hi Soraya,

The code will be fine only if 'data:post' exists. It is not the case in its actual form.

I didn't know the 'raw'  parameter for the url type. Very interesting ! I didn't find any information about it in your excellent documentation 
https://bloggercode.orbiona.com/search?q=raw&max-results=100

Have a nice day ! 

Manu Dhiman

unread,
Apr 30, 2025, 3:31:43 PMApr 30
to Forum Blogger Ambassade Francophone
Sorry, Guys

But if you could help me with this code, through the labels section, I want to display custom dates manually for my project.

<b:loop values='data:post.labels' var='label'>
  <b:if cond='data:label.name startsWith "updated:"'>
    <!-- Extract year, month, and day manually using conditions -->
    <b:with value='data:label.name' var='updatedLabel'>
      <!-- Extract year -->
      <b:if cond='data:updatedLabel.contains("-")'>
        <b:with value='data:updatedLabel.substring(8, 12)' var='year'/>
      </b:if>
     
      <!-- Extract month -->
      <b:if cond='data:updatedLabel.contains("-")'>
        <b:with value='data:updatedLabel.substring(5, 7)' var='month'/>
      </b:if>

      <!-- Extract day -->
      <b:if cond='data:updatedLabel.contains("-")'>
        <b:with value='data:updatedLabel.substring(0, 4)' var='day'/>
      </b:if>

      <!-- Display formatted date -->
      <b:if cond='data:month == "01"'>Last Updated: January <data:day/>, <data:year/></b:if>
      <b:if cond='data:month == "02"'>Last Updated: February <data:day/>, <data:year/></b:if>
      <b:if cond='data:month == "03"'>Last Updated: March <data:day/>, <data:year/></b:if>
      <b:if cond='data:month == "04"'>Last Updated: April <data:day/>, <data:year/></b:if>
      <b:if cond='data:month == "05"'>Last Updated: May <data:day/>, <data:year/></b:if>
      <b:if cond='data:month == "06"'>Last Updated: June <data:day/>, <data:year/></b:if>
      <b:if cond='data:month == "07"'>Last Updated: July <data:day/>, <data:year/></b:if>
      <b:if cond='data:month == "08"'>Last Updated: August <data:day/>, <data:year/></b:if>
      <b:if cond='data:month == "09"'>Last Updated: September <data:day/>, <data:year/></b:if>
      <b:if cond='data:month == "10"'>Last Updated: October <data:day/>, <data:year/></b:if>
      <b:if cond='data:month == "11"'>Last Updated: November <data:day/>, <data:year/></b:if>
      <b:if cond='data:month == "12"'>Last Updated: December <data:day/>, <data:year/></b:if>
    </b:with>
  </b:if>
</b:loop>

Christian DEHAIS

unread,
May 1, 2025, 11:07:59 AMMay 1
to Forum Blogger Ambassade Francophone
Hi Manu,

Before going further, what about your first question ? Did our answers help you ? In fact we are not AI, but humans ;)

Your second code uses terms that don't exist in Blogger's XML :  startsWith, substring.

As far as I know, the only way with Blogger's XML to extract something from a string is the quite limited operator 'snippet'
https://bloggercode.orbiona.com/2017/04/operator-snippet.html


Have a nice day !



Reply all
Reply to author
Forward
0 new messages