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.

Conditional orderby="{segment_x}" sort="{segment_(x+1)}"

March 28, 2013 7:13am

Subscribe [2]
  • #1 / Mar 28, 2013 7:13am

    maxi

    220 posts

    Hi, I was wondering if you could help me understand why this bit of code seems to have stopped working for me.

    In the exp:channel:entries tag I’ve inserted some conditionals to sort and order the entries depending on the url, and with the code below I want to order by price or year.

    Any reason why this would have stopped working?

    {if segment_2 == "price" OR segment_2 == "year"}
          orderby="{segment_2}" 
          sort="{segment_3}" 
    {/if}
      
    {if segment_4 == "price" OR segment_4 == "year"}
          orderby="{segment_4}" 
          sort="{segment_5}" 
    {/if}

    Thanks! 😊

  • #2 / Mar 28, 2013 8:29am

    Tyssen

    756 posts

    I’m surprised it ever worked in the first place. As soon as you put an AND or OR in your conditional, it becomes an advanced conditional and I’m pretty sure they don’t work wrapped around channel entries parameters, only simple conditionals do.

  • #3 / Mar 28, 2013 9:07am

    maxi

    220 posts

    Oh I see, hmmm, how would you suggest tweaking it?


    Tried this but doesn’t seem to work either

    {if segment_2 == "price"}
          orderby="{segment_2}" 
          sort="{segment_3}" 
         {/if}
      
         {if segment_4 == "price"}
          orderby="{segment_4}" 
          sort="{segment_5}" 
         {/if}
         
         {if segment_2 == "year"}
          orderby="{segment_2}" 
          sort="{segment_3}" 
         {/if}
      
         {if segment_4 == "year"}
          orderby="{segment_4}" 
          sort="{segment_5}" 
         {/if}

    Thanks

  • #4 / Mar 28, 2013 5:45pm

    Tyssen

    756 posts

    You could maybe try Stash or a preload_replace to set the variable outside the channel entries tag.

  • #5 / Mar 31, 2013 6:51pm

    Tyssen

    756 posts

    Actually here’s a blog post that describes your situation and the solution exactly: http://johndwells.com/blog/expressionengine-parse-order-advanced-conditionals-as-tag-parameters

  • #6 / Apr 01, 2013 6:15am

    maxi

    220 posts

    Ohhh that looks like it! 😊

    However I tried changing the tag to this, to capture url segments like ‘.../price/asc’ or ‘.../year/desc’ but it doesn’t seem to be working?

    {exp:channel:entries channel="orologi" uncategorized_entries="no" cache="yes" refresh="30"
    ...
    {exp:ifelse parse="inward"} 
         {if segment_2 == "price" OR segment_2 == "year"}
          orderby="{segment_2}"
          sort="{segment_3}"
         {/if} 
    {/exp:ifelse}
    }

    See anything amiss?

    Thanks!

  • #7 / Apr 01, 2013 6:19am

    Tyssen

    756 posts

    parse=“inward” should be on the channel entries tag, not the plugin.

  • #8 / Apr 01, 2013 8:15am

    maxi

    220 posts

    Hmm not yet… Here’s the full entries tag, only the bit in between {exp:ifelse} is returning no results:

    {exp:channel:entries channel="orologi" uncategorized_entries="no" cache="yes" refresh="30" parse="inward"
                  
                  {if segment_2 == "marca"}category="{segment_3_category_id}"{/if}
                  {if segment_4 == "marca"}category="{segment_5_category_id}"{/if}
                  {if segment_6 == "marca"}category="{segment_7_category_id}"{/if}
                  {if segment_8 == "marca"}category="{segment_9_category_id}"{/if}
      
         {if segment_2 == "materiale-cassa" OR segment_2 == "materiale-cinturino" OR segment_2 == "movimento" OR segment_2 == "colore-quadrante" OR segment_2 == "genere" OR segment_2 == "condizione"}
          search:{segment_2}="{segment_3}"
         {/if}
      
         {if segment_4 == "materiale-cassa" OR segment_4 == "materiale-cinturino" OR segment_4 == "movimento" OR segment_4 == "colore-quadrante" OR segment_4 == "genere" OR segment_4 == "condizione"}
          search:{segment_4}="{segment_5}"
         {/if}
      
         {exp:ifelse}    
    
         {if segment_2 == "prezzo" OR segment_2 == "anno"}
          orderby="{segment_2}" 
          sort="{segment_3}" 
         {/if}
      
         {if segment_4 == "prezzo" OR segment_4 == "anno"}
          orderby="{segment_4}" 
          sort="{segment_5}" 
         {/if}
         
    {/exp:ifelse}
        }
  • #9 / Apr 01, 2013 8:55am

    Tyssen

    756 posts

    I’d turn off cacheing while you’re still trying to troubleshoot. Also with so many different statements in there, you may be better off using Switchee instead which is also by Mark Croxton.

  • #10 / Apr 01, 2013 9:24am

    maxi

    220 posts

    Hmm good point about cache.

    Many different statements because I’m trying to filter/sort/order entries using url segments. Any better way to achieve that? 😊

  • #11 / Apr 01, 2013 10:28am

    maxi

    220 posts

    Hmmm, this doesn’t seem to be working either…

    {exp:switchee variable = "segment_2" parse="inward"}
     {case value="prezzo|anno"}
      orderby="{segment_2}" 
      sort="{segment_3}" 
     {/case}
    {/exp:switchee}
  • #12 / Apr 01, 2013 3:43pm

    maxi

    220 posts

    OK got it working, was missing single quotes around ‘{segment_x}’.

    Only one odd thing happening, it only works in the development environment, which is just a different channel that is not the default one, so it has one extra segment in the beginning, which i accounted for and reduced segment number by one, but still only working in development?

    orderby="{exp:ifelse}{if '{segment_2}' == 'prezzo' OR '{segment_4}' == 'prezzo'}prezzo{if:elseif '{segment_2}' == 'anno' OR '{segment_4}' == 'anno'}anno{if:else}date{/if}{/exp:ifelse}"
    
    sort="{exp:ifelse}{if '{segment_3}' == 'asc' OR '{segment_5}' == 'asc'}asc{if:else}desc{/if}{/exp:ifelse}"
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases