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.

Is there a cleaner way to conditionally show code in an entry loop if related entries or category assignments exist?

August 13, 2013 1:57pm

Subscribe [2]
  • #1 / Aug 13, 2013 1:57pm

    jaasum

    36 posts

    I ran into what I imagine is a common use case and I know there has to be a cleaner way to do this.

    {exp:channel:entries channel="media" limit="1" dynamic="no" paginate="bottom"}
     <article class="mediaList">
      <h2><a href="http://{url_title_path=media/detail}">{title}</a></h2>
    <p>  <dl></p>
    
    <p>   {media-speaker limit="1"}<br />
       <dt>Speaker</dt><br />
       <dd><br />
       {/media-speaker}</p>
    
    <p>   {media-speaker backspace="10"}<br />
       {media-speaker:people-first} {media-speaker:people-last}, <br />
       {/media-speaker}</p>
    
    <p>   {media-speaker limit="1"}<br />
       </dd><br />
       {/media-speaker}</p>
    
    <p>   {categories show_group="2" limit="1"}<br />
       <dt>Service</dt><br />
       <dd><br />
       {/categories}</p>
    
    <p>   {categories show_group="2" backspace="10"}<br />
       {category_name}, <br />
       {/categories}</p>
    
    <p>   {categories show_group="2" limit="1"}<br />
       </dd><br />
       {/categories}</p>
    
    <p>   {categories show_group="3" limit="1"}<br />
       <dt>Series</dt><br />
       <dd><br />
       {/categories}</p>
    
    <p>   {categories show_group="3" backspace="10"}<br />
       {category_name}, <br />
       {/categories}</p>
    
    <p>   {categories show_group="3" limit="1"}<br />
       </dd><br />
       {/categories}</p>
    
    <p>  </dl><br />
      {media-description}<br />
      <a href="http://">View this entry.</a><br />
     </article><br />
     {paginate}<br />
            Page {current_page} of {total_pages} pages {pagination_links}<br />
        {/paginate}<br />
     {/exp:channel:entries}

    In three places I’m forced to use the categories or related fields tag with a limit of 1 to show opening tags, closing tags, and headings. Is there an easier way to test “if related field” or “if category group # is assigned” instead of running multiple loops?

    I’m simply trying to have a list page that shows conditional meta information about each post (such as if speakers are assigned which sometimes may be multiple, if certain category groups and categories are assigned which are also sometimes multiples).

  • #2 / Aug 13, 2013 7:24pm

    Rob Allen

    3114 posts

    You can use conditional to check the first and last item, eg:

    <dl>
    
    {media-speaker}
    {if media-speaker:count == "1"}
    <dt>Speaker</dt>
    {/if}
    <dd>{media-speaker:people-first} {media-speaker:people-last}</dd>
    {/media-speaker}
    
    </dl>

    That will add the DT on the first media-speaker loop, then add in all the DD’ for each occurrence. If there are no speakers the tag won’t output anything.

    Use the same logic for other fields…

  • #3 / Aug 13, 2013 7:50pm

    jaasum

    36 posts

    That would work well for the relationship field, however the category variable pair doesn’t have count available as far as I know.

  • #4 / Aug 14, 2013 4:00pm

    Rob Allen

    3114 posts

    No count for categories (well not inside channel entries tag), but http://gwcode.com/add-ons/gwcode-categories addon will do it for you, you can assign it an entry_id and it will pump out assigned categories, and you can run a conditional inside the gwcats tag to pull in your DT in the first loop.

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

ExpressionEngine News!

#eecms, #events, #releases