feeling little dumb for this.

What I am trying to do is to make my list of checkboxes checked depending on {segment_5}

If the value is '6', that is easy with

<input type="checkbox" name="categories[]" value="{category_id}" {if '{segment_5}' == '{category_id}'}checked='checked'{/if} /> {category_name}

I am in a situation where my url goes like this 'site.dev/search/geo/n31lj/6-7' from doing a search.
Look at {segment_5}, the value is '6-7', multiple categories have been selected.

How can I tell the {category_id} to pick up 6 or 7 inside the {segment_5} with conditionals?

Look at this link, I have tried but it not possible.

2 Answers 2


There are a several ways really. You could use regular PHP:

$categories = "{segment_5}"; 

$category = explode("-", $categories);

echo "category 1 = $category[0]"; // 5
echo "Category 2 = $category[1]"; // 6

But beware of parse order when integrating with PHP tags.

Alternative you could always have some string fun. You can take advantage of the full array of PHP string functions such as explode() and split the URL segment by the dash creating an array values (5 and 6 in your case).

{exp:phpstringfun function="explode" par1="-" separator="|" reverse="y"}

Because segment variables are parsed very early in the rendering of the template this should work fine. I'm exploding segment 5 of the URL by the dash (-) and then replacing it with a pipe (|) character. You could then use that in your query to test for either 5 OR 6 by wrapping that in a Stash variable to call later on in your condtional:

{exp:stash:set name="categories" parse_tags="yes"}
    {exp:phpstringfun function="explode" par1="-" separator="|" reverse="y"}{segment_5}{/exp:phpstringfun} 

{if segment_5 == '{exp:stash:categories}'}

That will mark it as checked if segment_5 == '5|6', if that's what you want. Once you have it broken up though you can handle it in many ways.

  • Thanks for putting me into a new perspective, this is not really something I want. The segment value is currently '5|6', I need to have a conditional to act like a fuzzy manner, so my conditional should return like this segment_5 == '5' OR segment_5 == '6' Sorry I should have explained better Apr 19, 2013 at 7:16
  • With your code above, that is what I am trying to do. {exp:channel:categories channel="items" category_group="3"} <input type="checkbox" name="categories[]" value="{category_id}" {if {category_id} == '{exp:stash:categories}' }checked='checked'{/if} /> {category_name} <br /> {/exp:channel:categories} '5' works but '6' is ignored Apr 19, 2013 at 7:33
  • The PHP way allows you to split them out and retrieve them individually as demonstrated. I just provided a sample of both as I wasn't exactly sure if you wanted an EITHER OR or just individual values. So the segment will always contain either one or two values?
    – Ian Young
    Apr 19, 2013 at 8:35
  • Sorry, I feel like I am not getting anywhere. The segment will have one or several values. Such as /5-6 or /5-9-11 or /4 (Just making it up). I need to find a way of getting it into an individual value to get a checkbox checked. {exp:channel:categories channel="items" category_group="3"} <input type="checkbox" name="categories[]" value="{category_id}" {if ??? == ??? }checked='checked'{/if} /> {category_name} <br /> {/exp:channel:categories} Apr 19, 2013 at 9:12
  • To match the category_id Apr 19, 2013 at 9:25

== EDIT ==

{exp:stash:set name="numbers" parse_tags="yes"}
    {exp:low_replace find="-" replace="|"}{segment_4}{/exp:low_replace}

{exp:channel:categories channel="items" category_group="3"}
    <input type="checkbox" name="categories[]" value="{category_id}" {exp:loopee foreach="{exp:stash:numbers}" parse="inward"}{if category_id == {loopee_value}}checked='checked'{/if}{/exp:loopee} /> {category_name}<br />

This does exactly the job I wanted to do. Converting any segment with values like 5-19-11 into individual values to be checked with a conditional in order to make a checkbox checked

Plugins required
Low replace

  • Sorry, I can't always post an immediate response as notifications don't come through right away. But the combination between PHP and something like PHPStringFun as I mentioned was pretty much all you need. Once you split the segment up you can call in those variables as and when you want. This is exactly what I showed in my examples without wrapping it in a foreach loop for the input field (as that wasn't part of the question).
    – Ian Young
    Apr 19, 2013 at 11:26
  • Thanks Ian for not making me give up ;), the php solution was throwing errors. Apr 19, 2013 at 16:15
  • Nicely refined. :) That's an even better way of doing it. Keeping PHP out of templates is always preferable where possible just for extra security.
    – Ian Young
    Apr 19, 2013 at 18:18

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.