5

I have a conditional which is the following:

{if '{current_time format="%H%i"}' >= '0000' AND '{current_time format="%H%i"}' <= '1300'}
    <a href="{path="speisekarten"}#mittagskarte">{navi_speisekarten}</a> 
{if:else}
    <a href="{path="speisekarten"}#abendkarte">{navi_speisekarten}</a>
{/if}

This works well. But I need an additional function: If there is no entry in the Channel "mittagskarte" show <a href="{path="speisekarten"}#abendkarte">{navi_speisekarten}</a> even if it's between 0000 and 1300

How do I write this? This melts my brain. I tried a few things mostly ending up in a parsing errors.

The conditional is not inside a Channel Tag.

Thank you

1

2 Answers 2

11

I am not sure if i got it right but couldn't you simply wrap the conditionals in a {exp:channel:entries} tag and use the no_results to display your link?

Perhaps not the most optimal way but it should get you what you want.

Something like this would do...

{exp:channel:entries channel="mittagskarte" limit="1" dynamic="no" disable="categories|category_fields|custom_fields|member_data|pagination"}
    {if '{current_time format="%H%i"}' >= '0000' AND '{current_time format="%H%i"}' <= '1300'}
        <a href="{path="speisekarten"}#mittagskarte">{navi_speisekarten}</a> 
    {if:else}
        <a href="{path="speisekarten"}#abendkarte">{navi_speisekarten}</a>
    {/if}
    {if no_results}
        <a href="{path="speisekarten"}#abendkarte">{navi_speisekarten}</a>
    {/if}
{/exp:channel:entries}

Update: @foamcow mentions switchee, the same developer also has a plugin called IfElse which enables you to parse advanced conditionals early.
But i don't think you would need to since {if no_results} should be the only thing parsed in the channel-loop since the time-conditional, being an advanced conditional, is parsed later.

1
  • No probs. :D Would you mind marking this as answered? Nov 30, 2012 at 12:03
2

Put your channel check outside the time check.

In pseudo-code, something like..

If no entry in channel mittagskarte

    show link to abendkarte

else

    if time is between 0000 and 13000
        show first link
    else
        show second link
    end time check

end channel check

So you'll need a channel entries tagpair in there in order to test whether there are any entries in mittagskarte

{exp:channel:entries channel="mittagskarte" dynamic="off" limit="1"}
    {if no_results}
        <a href="{path="speisekarten"}#abendkarte">{navi_speisekarten}</a>
    {/if}

    {if '{current_time format="%H%i"}' >= '0000' AND '{current_time format="%H%i"}' <= '1300'}
        <a href="{path="speisekarten"}#mittagskarte">{navi_speisekarten}</a> 
    {if:else}
        <a href="{path="speisekarten"}#abendkarte">{navi_speisekarten}</a>
    {/if}       
{/exp:channel:entries}

I think you could probably use a couple of add-ons here if you really wanted to. I'm sure that Croxton's Switchee, or another "advanced conditionals" add on might help and you may even find another that will let you test the entry count in a channel without the tag pair. It should be simple enough write a plugin for that if one doesn't exist.

EDIT As pointed out by Kim, the no_results tag would be better than checking count.

2
  • This approach seems logic to me. But when I implement this the link doesn't show up at all when ´channel="mittagskarte"´ is empty. I tried to replace {if count != 0 } with {if title == ""} but that doesn't change anything.
    – KSPR
    Nov 30, 2012 at 11:56
  • See my update and use no_results rather than count - as per Kim's answer. See if that works.
    – foamcow
    Nov 30, 2012 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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