0

I have an events channel that displays brief information about upcoming events. Those entries are set to 'expire' when they pass a certain date. When there aren't any pending events I would like to display alternative text like “There are no upcoming events at this time. Sign up for a our Newsletter to keep up to date.”

I tried the following but it does not work because there isn't a case where there aren't any entries at all:

{if total_entries != 0}
<h2>{title}</h2>
<p>{event_dates}<br />
    {event_location}</p>
    {event_summary}
{if:else}
<p>There are no upcoming events at this time. Sign up for a our <a href="/contact/newsletter">Newsletter</a> to keep up to date.    </p>
{/if}   

Is there a way to set a condition for when there aren't any entries that aren't expired?

3

The reason your code is not working is because if ExpressionEngine does not return any results, then it doesn't even parse the tags - thus {total_entries} does not even exist in that context. Also note - it is {total_results}, not {total_entries}.

To help with this issue, EE has developed the {if no_results} conditional. Using that, your code should look as follows...

{if no_results}
     <p>There are no upcoming events at this time. Sign up for a our <a href="/contact/newsletter">Newsletter</a> to keep up to date.    </p>
{/if}
<h2>{title}</h2>
<p>
    {event_dates}<br />
    {event_location}
</p>
{event_summary}

ExpressionEngine will automatically count anything not within the 'no_results' tag as displaying if there ARE results.

| improve this answer | |
  • Please expand on your answers (in the future) besides providing a simple code suggestion. Also, please use code blocks and other markdown in the future to make your answer's presentation more parse-able. – jrothafer Jan 9 '16 at 3:02
  • Hi Jason, exactly what I needed. Interesting it is not found in the Conditional documentation. Problem solved. Thank you. Also, the documentation uses total_entries: docs.expressionengine.com/latest/templates/… – forrest Jan 10 '16 at 4:57
  • total_entries, I believe, gives you a count of all the entries on the site while total_results give you a count of how many are returned in a tag pair such as channel entries. – Jason Boothman Jan 10 '16 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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