1

I am trying to figure out a way to set to variable values depending on if a specific segment is a number or not. First how to I check if the segment 2 is a number or not? I want to apply this type of logic

if segment_2 is a number
    {exp:stash:set name="this_url_title" value=""}
    {exp:stash:set name="this_entry_id" value="{segment_2}"}
else
    {exp:stash:set name="this_url_title" value="{segment_2}"}
    {exp:stash:set name="this_entry_id" value=""}
end

Then use the 2 variables in a channel entries tag like so.

{exp:channel:entries channel="news" dynamic="off" entry_id='{exp:stash:get name="this_entry_id"}' url_title='{exp:stash:get name="this_url_title"}' parse="inward"}

So I can either pass an entry_id or a url_title in the segment to view the channel entry.

I tried hard coding the stash variables and passing them in the entries tag but I get no results. I tried hard coding them to this:

{exp:stash:set name="this_url_title"}{/exp:stash:set}
{exp:stash:set name="this_entry_id"}123{/exp:stash:set}
4

Using the matches operator, which was added in EE v2.9.0:

{if segment_2 ~ "/^\d+$/"}
    [...]
{/if}
3
  • 2
    Good call, for reference the matches operator was added in EE v2.9.0 – BrandonOHara Apr 9 '15 at 1:25
  • Cool, I was not aware you could do regular expression with comparators. I ended up with: {if segment_2 ~ "/^\d+$/"} without the / at the beginning and the end I got error: Something has gone wrong and this URL cannot be processed at this time. – Trevor Orr Apr 9 '15 at 2:11
  • I'm sorry about it! I simply forgot! I'm sorry! – Sobral Apr 9 '15 at 2:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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