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.

If a channel entry has no category

October 18, 2010 4:27pm

Subscribe [2]
  • #1 / Oct 18, 2010 4:27pm

    Burke Museum

    1 posts

    I am trying to write a conditional statement within the template code that will output different html if no category is selected. I can’t figure out how to write a conditional statement to see if no categories are selected.

    For example, I’m trying to accomplish something like this:

    {if categories}
      {categories limit="1"}
        {category_name}
      {/categories}
    {if:else}
      Event
    {/if}


    If a category has been sent it will display the category name, if not it will simply display “Event.” Is this something I can accomplish in the templating system, or am I going to have to use inline php?

  • #2 / Oct 19, 2010 3:51am

    John Henry Donovan

    12339 posts

    Kevin,

    Welcome to the forums 😊

    What version and build of EE are you using?

    This code is inside your channel:entries tag? Can you show us some more of the template?
    What does your URL look like for a category page

    Is this conditional to show a category name or Event for a number of listings or just once at the top of the page?

  • #3 / Oct 19, 2010 3:04pm

    Burke Museum

    1 posts

    Hi, thanks for your help.


    I’m using ExpressionEngine v2.1.0 Build:  20100810.

    Here’s a little bit more of the template code:

    {exp:channel:entries channel="events" start_on="{current_time format='%Y-%m-%d 00:00'}" orderby="entry_date|event_enddate" sort="asc" show_future_entries="yes"}
      <h3>
        {if categories}
        {categories limit="1"}
          {category_name}
        {/categories}
       {if:else}
        Event
       {/if}
      </h3>
    
    <p>  <div class="bk_event_description"><br />
        </p><h2>{title}</h2>
    <p>    {event_description}<br />
      </div><br />
    {/exp:channel:entries}

    I’m not particularly concerned with separate category pages as of yet. This is simply to display the type / category of event for each event. eg: Lecture, Workshop, Tour, etc. If there is not category event it should simply be labeled as an event.

    Right now the result of the code above is that ‘Event’ is always displayed in the <h3> element because the {if categories} conditional never returns true.

  • #4 / Oct 20, 2010 4:38am

    John Henry Donovan

    12339 posts

    Kevin,

    There is no shortened conditional for categories like that at the moment. It would make a good feature request though.

    The only thing I can think of here is to make a category called Event and make this the default in your channel preferences. That way if no other category is chosen it will always show Event at least.

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

ExpressionEngine News!

#eecms, #events, #releases