ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Conditional To Not Display Heading If No Categories

September 30, 2012 11:39am

Subscribe [2]
  • #1 / Sep 30, 2012 11:39am

    Gareth Redfern

    86 posts

    I think this should be a really simple one, I am displaying a list of categories in a sidebar of a news page using the categories tag. I have set the show_empty=“no” so that any empty categories do not display, but i want to set a conditional to not display the static heading above if there are no categories displaying in the navigation, ideally I would like to hide the section tags as well, this is the code I have at the moment:

    <section>
        <h2>News Categories</h2>
    <p>    {exp:channel:categories channel="news_articles" show_empty="no"}<br />
        <a href="http://{path=news}">{category_name}</a><br />
        {/exp:channel:categories}<br />
    </section>

    I am thinking I will need to wrap the section tags and heading in either a channel entries tag or categories tag to then set the conditional but this seems quite over the top to do such a simple thing.

    Any other ideas would be great.

    Gareth

  • #2 / Sep 30, 2012 3:13pm

    Rob Allen

    3105 posts

    You should be able to use a conditional here:

    {exp:channel:categories channel="news_articles" show_empty="no"}
    {if count == "1"}
      <section>
      <h2>News Categories</h2>
    <p>{/if}<br />
    <a href="http://{path=news}">{category_name}</a><br />
    {if count == total_results"}<br />
      </section><br />
    {/if}    <br />
    {/exp:channel:categories}

  • #3 / Sep 30, 2012 4:16pm

    Gareth Redfern

    86 posts

    Thanks for this Rob, it almost works perfectly. The only problem now is that the section and heading are output wrapped in an unordered list. I have set the style to linear and added the ul’s between the conditional and it seems to work great now. My code is:

    {exp:channel:categories channel="news_articles" show_empty="no" style="linear"}
      {if count == "1"}
      <section>
        <h2>News Categories</h2>
    <p>    <ul><br />
       {/if} <br />
        <li><a href="http://{path=news}">{category_name}</a></li><br />
      {if count == "total_results"}<br />
        </ul><br />
      </section><br />
      {/if}<br />
      {/exp:channel:categories}

    Thank’s for the help, I haven’t used the count variable very much and that is a clever use of it.

     

  • #4 / Sep 30, 2012 4:49pm

    Rob Allen

    3105 posts

    Ah yes, sorry I missed “linear” output! At least you got there 😊

    The count variable is very useful!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases