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.

If segment equals url_title conditional

November 02, 2011 5:33pm

Subscribe [3]
  • #1 / Nov 02, 2011 5:33pm

    bcrabtree

    60 posts

    I’m wondering if it is possible to check if a url segment is a url_title outside the channel:entries tag.

    For example if I want to display the same channel with different parameters depending on if I’m viewing an entry list or single entry page.

    So:

    {if segment_1 == any_url_title}
       {exp:channel:entries channel="channel_1" dynamic="no" url_title="{segment_1}"}
          {title}
       {/exp:channel:entries}
    {if:else}
       {exp:channel:entries channel="channel_1" dynamic="no" limit="10"}
          {title}
       {/exp:channel:entries}
    {/if}

    This would result in a situation where if segment 1 equals a url title (any url title) then it returns the first entries loop, and if segment 1 does not equal a url title the it returns the second entries loop.

    This is mainly the result of having to use dynamic=“no” in my entries loop.

    Possible?

    Moved to Community Help forum by Moderator

  • #2 / Nov 03, 2011 12:00pm

    Kevin Cupp

    791 posts

    Hi Bryan,

    Yes this is possible. Here’s how I would do it:

    {if segment_1 != ''}
       {exp:channel:entries channel="channel_1" dynamic="no" url_title="{segment_1}"}
          {title}
       {/exp:channel:entries}
    {/if}
    {if segment_1 == ''}
       {exp:channel:entries channel="channel_1" dynamic="no" limit="10"}
          {title}
       {/exp:channel:entries}
    {/if}

    We can’t really tell if a segment is a URL title or not, so we have to assume if there is anything in segment_1, then it must be a URL title. Also, I changed the conditional to be two separate if statements instead of an if:else. That’s because adding if:else makes the conditional an advanced conditional, and it would cause both channel:entries tags to parse even though only one needs to parse (more on this on the url=http://expressionengine.com/wiki/Parse_Order]parse order wiki page[/url]). By sticking to a simple conditional, we can make sure only the channel:entries that needs to run is run, giving us better performance.

    Also, you may not need a conditional, you may want to try this to simplify things:

    {exp:channel:entries channel="channel_1" dynamic="no" limit="10" url_title="{segment_1}"}
        {title}
    {/exp:channel:entries}

    That way, if a url_title is present, it should force channel:entries to only show a single entry, but show 10 entries if there isn’t an url_title.

    Let us know if that answers your question.

    Kevin

  • #3 / Nov 03, 2011 2:16pm

    bcrabtree

    60 posts

    Hi Kevin,

    Thank you for your response. Unfortunately that method doesn’t work for me because I’m using pagination. So if segment_1 equals a pagination segment (P0, P10, etc) an error occurs because P10 is not a url_title. Which is why I’m trying to check if segment_1 is in fact an actual url_title before doing anything.

    I think I may have found a solution using the Switchee plug in. I haven’t tested fully but I thinks it’s going to work for what I’m trying to accomplish.

    Cheers,
    Bryan

  • #4 / Nov 03, 2011 10:59pm

    Sue Crocker

    26054 posts

    Hi, Bryan. Since you’re going to be testing with Switchee, I’m moving this to the Community Help forum for additional community support.

  • #5 / Nov 03, 2011 11:17pm

    bcrabtree

    60 posts

    Ok, sounds good. Switchee solved my problem.

    Cheers,
    Bryan

  • #6 / Nov 03, 2011 11:33pm

    Sue Crocker

    26054 posts

    Yay! Glad to hear it. 😊

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

ExpressionEngine News!

#eecms, #events, #releases