Can you set your blog to show only the very first label of a post in Blogger?
Asked Answered
P

2

6

I have some experience in HTML and CSS but proper coding like Java, JS and PHP I am a novice to, plus this is the first time that I have built a Blogger Template/Site from scratch so it is a lot of trial and error. I have tried looking for an answer but I couldn't find an answer that I felt was relevant to what I was looking for.

As standard we know that the Blog1 widget will show your most recent post in its entirety on the landing page along with the author details, comments and labels in the footer. My question is can you alter the code to call for only the first label to be displayed at the bottom of the post?

As standard this is the syntax used to call for the labels list within the native blogger coding:

 <b:if cond='data:top.showPostLabels and data:post.labels'>
   <data:postLabels/>
   <b:loop values='data:post.labels' var='label'>
     <a expr:href='data:label.url' rel='tag'><data:label.name/></a>
     <b:if cond='not data:label.isLast'>,</b:if>
   </b:loop>
</b:if>

I though that maybe altering the b:loop to say a b:include would stop blogger calling for more of the labels and would maybe stop at just one, but when I changed this it basically stopped the entire blog from being displayed!

All I want is for it to be "Labels: Label1" instead of "Labels: Label1, Label2, Label3, etc" without resorting to limiting the amount of labels to just one.

Thanks in advance to anyone who replies and helps.

Mark

Presser answered 10/2, 2017 at 8:53 Comment(0)
K
4

You can use index attribute which gives zero-based index of post labels through the loop

<b:if cond='data:top.showPostLabels and data:post.labels'>
    <data:postLabelsLabel/>
    <b:loop values='data:post.labels' index='i' var='label'>
        <b:if cond='data:i == 0'>
          <a expr:href='data:label.url' rel='tag'>
            <data:label.name/>
          </a>
        </b:if>
    </b:loop>
</b:if>
Kamikamikaze answered 10/2, 2017 at 15:34 Comment(0)
S
1

Update: A much simpler way to achieve what you require can be done via this code -

<data:postLabelsLabel/> <a expr:href="data:post.labels[0].url" ><b:eval expr='data:post.labels[0].name'/></a>

An additional conditional statement will be required to limit the labels to just one. The above code will be modified as follows -

<b:if cond='data:top.showPostLabels and data:post.labels'>
    <data:postLabelsLabel/>
    <b:loop values='data:post.labels' var='label'>
        <b:if cond='data:label.isLast'>
            <a expr:href='data:label.url' rel='tag'>
                <data:label.name/>
            </a>
        </b:if>
    </b:loop>
</b:if>

I have used the conditional data:label.isLast to print the label only if it is the last one in the post (in case there is only one label, then that would be printed). Blogger arranges the labels in alphabetical order and doesn't provide any control over changing the sorting order.

For example, if the post has the labels as follows "Labels: A, B, C", then after changing the code to the one above, the end result would be "Labels: C"

Shoddy answered 10/2, 2017 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.