tjsxs
tjsxs

Reputation: 29

Define loop count in blogger xml

<b:if cond='data:post.labels'>
  <b:loop values='data:post.labels' var='label'>
    <data:label.name/>
    ,
  </b:loop>
</b:if>

This loop lists all the labels in a post,

is it possible to limit the number of labels shown by controlling the loop (defining a loop count)?

Eg: There are total 7 labels in a post but i want only the first 3 to be displayed.

Upvotes: 3

Views: 2829

Answers (5)

Prayag Verma
Prayag Verma

Reputation: 5651

It is possible to directly add the limit keyword followed by the number to the values field

<b:if cond='data:post.labels'>
  <b:loop values='data:post.labels limit 3' var='label'>
    <data:label.name/>
    ,
  </b:loop>
</b:if>

Upvotes: 2

Younesse Bagachoul
Younesse Bagachoul

Reputation: 101

Here is a simple solution :

<b:if cond='data:post.labels'>
  <b:loop values='data:post.labels' var='label' index='sth'>
    <b:if cond='data:sth&lt;3'>
    <data:label.name/> ,
    </b:if>
  </b:loop>
</b:if>

In blogger : < = &lt; and > = &gt;

And I've used this statment : sth&lt;3 Which means sth<3, so that the loop will repeat 3 times, hence, 3 entries should show up (Because the loop starts from 0)

You could also create a range adding new statment as shown below :

<b:if cond='data:post.labels'>
  <b:loop values='data:post.labels' var='label' index='sth'>
    <b:if cond='data:sth&lt;6 and data:sth&gt;2'>
    <data:label.name/> ,
    </b:if>
  </b:loop>
</b:if>

The code above will show 3 labels picked up from the 3rd loop to the 5th loop.

Upvotes: 0

wj R.
wj R.

Reputation: 359

This one will show your first three labels (count starts from zero). 2 commas are also included, no comma for the third label.

<b:loop values='data:post.labels' var='label' index='x'>
  <b:if cond='data:x &#60;=2'>
    <a class='sfa-tag' expr:href='data:label.url' rel='tag'>
      <data:label.name/>
    </a>
    <b:if cond='data:x &#60;=1'>,</b:if>
  </b:if>
</b:loop>

Upvotes: 0

AlfredoAbambres
AlfredoAbambres

Reputation: 11

This works for me:

<b:if cond='data:post.labels'>
  <b:loop values='data:post.labels' var='label' index='x'>
    <b:if cond='data:x==0'><data:label.name/>, </b:if>
    <b:if cond='data:x==1'><data:label.name/>, </b:if>
    <b:if cond='data:x==2'><data:label.name/></b:if>
  </b:loop>
</b:if>

Or simply use < or > (encoded as &lt; and &gt; respectively):

<b:if cond='data:post.labels'>
  <b:loop values='data:post.labels' var='label' index='x'>
    <b:if cond='data:x&lt;=2'><data:label.name/>, </b:if>
  </b:loop>
</b:if>

Upvotes: 1

Ifan Iqbal
Ifan Iqbal

Reputation: 3093

No, it is impossible to create new variable data and assign value to it in Blogger template XML. There is no documentation from Google that give information about it. Blogger template XML is not designed for it.

Upvotes: 0

Related Questions