0

Is it possible to use a count conditional (or equivalent conditional) to change the display of an entry tag depending on how many entries there are?

For example, I have a grid field which I am using for a simple image gallery. I would like to add a condition so that if there is only one entry the image will display as a hero banner, but if there is more than one entry it will change to a gallery.

I wasn't sure if the following (or similar) was possible:

{if count > 1}
   <!-- Gallery -->        
{if:else}
   <!-- Hero Image -->          
{/if}

Thanks for any help.

1
  • I literally just woke up, but I'm certain you can do what you're asking. If no one picks this up, I'll have an answer for you in a few hours. – jrothafer Jul 3 '15 at 11:12
0

Certainly, what you want to do is this :

{grid_field}
  {if total_rows < 2}
    {!-- Hero Banner goes here --}
  {if:else}
    {!-- Each Gallery Image displayed here --}
  {/if}
{/grid_field}

Note that for what you're asking (if more than one do X else do Y) on a grid field you will need to use the {total_rows} parameter (or for channel entries its {absolute_results}).

Also, note the parse order. Simple conditionals are parsed before module tags, advanced ones after. I don't think it makes a different but if you're not getting the right results change the {if total_rows < 2}{if:else}{/if} to a {if total_rows < 2}{/if}{if total_rows > 1}{/if}

2
  • 1
    Bastard! :P I wanted to claim this one :) – jrothafer Jul 3 '15 at 12:05
  • ROFL, I so almost put a comment about sniping your awnswer too JChrono ! :P lol, let me find a few of your other answers, a few up votes will soon correct the point difference (PS> I know what your doing, you know I've not been able to get on here much lately, trying to de-throne me on this months points table!) hahahaha – Blatant Jul 3 '15 at 16:27

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.