Search code examples
conditional-statementsexpressionengine

How can I use a conditional to check whether a channel has any entries in it?


I have a Conditional Outside a Channel entries tag which should determin if a Channel has 1. Entries at all, 2. Expired entries, 3. Closed entries: I tried {if channel_short_name == "news"} But somehow that returns the wrapped content no matter if entries are closed or expired. The reason that I have the conditional is outside the channel tag is that I dont want to repeat the h2 tag.

{if there are a displayable entries in the "news" channel, display this whole package.}
   <h2>News</h2>
   <hr />
   {exp:channel:entries channel="news" limit="2"}
      <div class="entry panel">
         <h3>{title}</h3>
         {news_text}
         {if news_full OR news_bild}
            <div id="{entry_id}" class="toggleDiv">
               {news_full}
               {exp:ce_img:single src="{news_bild}" max_width="346" smart_scale="yes" alt="{name_orig}"}
            </div>
            <p><a class="show_hide" rel="#{entry_id}" href="#">Mehr…</a></p>
        {/if}
      </div>
   {/exp:channel:entries}
{/if}

This brings me to another question:

Is it possible to set expired entries to "closed"?


Solution

  • The simplest option would be to move your header markup into a conditional that is only displayed with the first entry. If there are no results then the {exp:channel:entries} tag will generate no output.

    {exp:channel:entries channel="news" limit="2"}
        {if count == '1'}
            <h2>News</h2>
            <hr />
        {/if}
        <div class="entry panel">
            <h3>{title}</h3>
            {news_text}
            {if news_full OR news_bild}
                <div id="{entry_id}" class="toggleDiv">
                    {news_full}
                    {exp:ce_img:single src="{news_bild}" max_width="346" smart_scale="yes" alt="{name_orig}"}
                </div>
                <p><a class="show_hide" rel="#{entry_id}" href="#">Mehr…</a></p>
          {/if}
        </div>
    {/exp:channel:entries}
    

    Is there any particular reason why you want to close expired entries? Unless you're using show_expired = 'yes' all expired entries will behave as if they're closed anyway.