1

I have one particular page that is being sorted according to the 3rd segment. If that segment doesn't exist, I'd like to have orderby revert to it's default. Basically, if {segment_3} doesn't exist, then sort this way. How would I acheive this?

   {exp:channel:entries channel="news" orderby="{segment_3}|author_id" sort="desc" limit="9" paginate="bottom"}

or

{exp:channel:entries channel="news" orderby="{segment_3} OR author_id" sort="desc" limit="9" paginate="bottom"}

1 Answer 1

4

You can wrap channel:entries tag parameters in simple conditionals. So a conditional based on a segment variable will work.

This is just an example since I'm not entirely sure I interpreted what you're after, but should give you an idea.

{exp:channel:entries 
    channel="news" 
    limit="9" 
    paginate="bottom"
    sort="desc"
    {if segment_3}
        orderby="{segment_3}"
    {/if}
    {if segment_3 == ""}
        orderby="author_id"
    {/if}
}
    {title}<br>

{/exp:channel:entries}

Now that I think about it, though, since pagination is involved you will run into some troubles with this method. Because even when segment_3 is blank to begin with, segment_3 won't be blank on subsequent pages (it will by P10, P20, etc).

There are other ways to accomplish this, though. Switchee should do the trick:

{exp:switchee variable = "{segment_3}" parse="inward"}

    {case default="yes"}
        {exp:channel:entries 
            channel="news" 
            limit="9" 
            paginate="bottom"
            sort="desc"
            orderby="{segment_3}"
        }
            {title}<br>

        {/exp:channel:entries}
    {/case}

    {!-- segment_3 is pagination or empty --}
    {case value="#^P(\\d+)$#|''"}
        {exp:channel:entries 
            channel="news" 
            limit="9" 
            paginate="bottom"
            sort="desc"
            orderby="author_id"
        }
            {title}<br>

        {/exp:channel:entries}
    {/case}

{/exp:switchee}

For the segment_3-based sorting to work you'd obviosuly want to be sure that segment_3 contained a valid value for the channel:entries order_by parameter.


OR if this seems like too much segment-based tomfoolery...

Your suggestion in the comments to use GET variables is a good idea. Mo' Variables will work great for that. So with a URL of http://example.com/foo/bar?orderby=title you could do:

{exp:channel:entries 
    channel="news" 
    limit="9" 
    paginate="bottom"
    sort="desc"
    {if get:orderby}
        orderby="{get:orderby}"
    {/if}
}
    {title}<br>

{/exp:channel:entries}
2
  • Thanks Alex, this is a great answer. You are right, the pagination causes the first solution to run into errors. I am sure your second proposed solution will work just fine, but perhaps I should be asking another question. Should I be using GET params maybe to handle the sort/orderby arguments? I feel like relying on the URL structure for these can be problematic for me in the future on other pages. Thanks again for taking the time to respond!
    – Chris M
    Commented Oct 30, 2013 at 21:17
  • Yeah, sometimes this can start you down the road of jumping through too many segment-based hoops in your templates. GET params is a great idea! Mo' Variables (add early parsed global variables, including GET) will be indispensable if you want to go that route. I updated my answer with a note on that.
    – Alex Kendrick
    Commented Oct 30, 2013 at 21:26

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.