Search code examples
labelbloggerblogs

Display only one label of a group (Blogger)


I'm trying to set the visualization of a label in home page but I've some problems. What I want is to visualize only one label also for the posts that have more labels. This is the code:

<b:if cond='data:view.isMultipleItems != data:view.isLabelSearch'>
<b:loop values='data:post.labels' var='label'>
<b:if cond='data:label.name == &quot;A&quot; or data:label.name == &quot;B&quot; or data:label.name == &quot;C&quot; or data:label.name == &quot;D&quot; or data:label.name == &quot;E&quot; or data:label.name == &quot;F&quot;'>
<span> <a expr:href='data:label.url' rel='tag'><data:'label.name' /></a> </span>
</b:if>
</b:loop>
</b:if>

I tried also this one, but some labels aren't displayed:

<b:if cond='data:view.isMultipleItems != data:view.isLabelSearch'>          
<b:loop values='data:post.labels limit 1' var='label'>
<b:if cond='data:label.name == &quot;A&quot; or data:label.name == &quot;B&quot; or data:label.name == &quot;C&quot; or data:label.name == &quot;D&quot; or data:label.name == &quot;E&quot; or data:label.name == &quot;F&quot;'>
<span> <a expr:href='data:label.url' rel='tag'><data:'label.name' /></a> </span>
</b:if>
</b:loop>
</b:if>

Solution

  • You don't need to loop for labels since you're going to show just one label:

    <b:if cond='data:view.isMultipleItems and not data:view.isLabelSearch'>          
      <b:if cond='data:post.labels.size gt 0'>
        <span><a expr:href='data:post.labels.first.url' rel='tag'><data:post.labels.first.name/></a></span>
      </b:if>
    </b:if>
    

    edit after comments:

    <b:if cond='data:view.isHomepage'> 
      <b:if cond='data:post.labels'>
        <b:with value='data:post.labels where (l => l.name in ["A","B","C","D","E","F"]) limit 1' var='group'>
          <span><a expr:href='data:group.first.url' rel='tag'><data:group.first.name/></a></span>
        </b:with>
      </b:if>
    </b:if>