最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can you set your blog to show only the very first label of a post in Blogger? - Stack Overflow

programmeradmin2浏览0评论

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, ments 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

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, ments 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

Share Improve this question edited Feb 10, 2017 at 10:37 M Hamza Javed 1,2654 gold badges18 silver badges32 bronze badges asked Feb 10, 2017 at 8:53 PhoenixTWPhoenixTW 1131 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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>

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"

发布评论

评论列表(0)

  1. 暂无评论