5

In the margin of my blog, I list featured articles. This is the code. My problem: I don´t always have stuff to feature. When the channel entries tag is empty a grey box with the title Featured is left over. I dont want to use a conditional, that says "if no entry, then print "Nothing to feature yet"."
I want to get rid of the entire box. How can i do this?

<div class="panel">
  <h5>Featured</h5>
  {exp:channel:entries channel="blog" limit="2" status="featured"}
    <p><a href="{url_title_path='eintrag/index'}">{title}</a></p>
  {/exp:channel:entries}
</div>

1 Answer 1

25

Bring the <div> inside your channel entries tag and do something like this:

{exp:channel:entries channel="blog" limit="2" status="featured"}
  {if count == 1}
  <div class="panel">
    <h5>Featured</h5>
  {/if}
      <p><a href="{url_title_path='eintrag/index'}">{title}</a></p>
  {if count == total_results}
  </div>
  {/if}
{/exp:channel:entries}

ie, output the opening <div> and <h5> only on the first item in the loop, and only output the closing <div> on the last item in the loop. This way no content is output if there are no results.

You can also optionally handle the no_results condition specially using the {if no_results} conditional.

3
  • Np. If you're sure that answers your question it would be great if to click the arrow underneath the vote controls to mark the answer as accepted.
    – Tom Davies
    Nov 29, 2012 at 13:26
  • I'm curious: why limit="2" and not limit="1"? I've been having a problem with a similar situation where I have limit="1" not working.
    – edward
    May 4, 2017 at 3:50
  • @edward I purely used limit="2" as that was what the questioner used in their code example - limit="1" should work too, though if you only want one entry you shouldn't need any count based conditionals inside the loop anyway, as the loop will only run through once
    – Tom Davies
    May 5, 2017 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.