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.

Why won't this conditional statement work?

November 05, 2012 10:11am

Subscribe [3]
  • #1 / Nov 05, 2012 10:11am

    e-finity

    22 posts

    I can’t for the life of me work out why this doesn’t work so I thought I’d ask –

    I’ve got a template which either shows a list of entries (max 10)

    mydomain.com/blog

    Paginates

    mydomain.com/blog/P1 etc…

    Or displays the entry

    mydomain.com/blog/displays-the-entry-associated-with-this-url-title

    The template is as follows –

    {if segment_2 == '' OR segment_2 == 'P1' etc…}
    {exp:channel:entries channel="blog" dynamic="no" limit="10" paginate="bottom" }
    etc…
    {/exp:channel:entries}
    {if:else}
    {exp:channel:entries channel="blog" dynamic="no" url_title="{segment_2}"}
    etc…
    {/exp:channel:entries}

    This works fine but obviously a person can type whatever they want into segment 2 which gives a poor experience when the page doesn’t exist. To prevent this I’d traditionally just add the following to the second channel entries tag:

    {if no_results}
    {redirect="/404"}             
    {/if}

    The problem is this is 404’ing the pagination URL’s. I can’t understand why the conditional statement is allowing this when it doesn’t allow the rest of the code between it.

    I’ve been playing with it for several hours but I’m out of ideas, anyone know?

  • #2 / Nov 05, 2012 10:25am

    I think this is because of the parsing order.
    an if/else structure is an advanced conditional and is being parsed AFTER your channel entries tag.

    Which means that no matter what is in your segment_2, your second pain of channel entries tags will be parsed.

    So in the case of your pagination, it loops through your first pair ( all goes well ) but then it loops through the second pair, resulting in “no results” and redirecting the page.

    The solution might be to replace your if/else structure with 2 simpel ifs, because those will get parsed BEFORE the channel entries.
    So something along the lines of:

    {if segment_2 == '' OR segment_2 == 'P1' }
      {exp:channel:entries channel="blog" dynamic="no" limit="10" paginate="bottom" }
        etc…
      {/exp:channel:entries}
    {/if}
    {if segment_2 != '' AND segment_2 != 'P1' }
      {exp:channel:entries channel="blog" dynamic="no" url_title="{segment_2}"}
        etc…
      {/exp:channel:entries} 
    {/if}
  • #3 / Nov 05, 2012 10:41am

    e-finity

    22 posts

    You’re right!

    I wish I’d asked two keyboards ago…

    Thanks.

  • #4 / Nov 06, 2012 3:48pm

    Shane Eckert

    7174 posts

    Hey e-finity,

    I am glad that Jan was able to help! That’s awesome.

    If you need anything else, please just let me know by opening a new thread.

    Cheers,

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

ExpressionEngine News!

#eecms, #events, #releases