2

I am using channel form to allow users to add a new entry. I need to output a category to allow then to select multiple categories and sub categories. The following code works but it does not order the sub cats below the parent Category:

<select name="category[]" size="100" multiple="multiple" id="multiSelect">
{exp:query sql="SELECT cat_name, cat_id FROM exp_categories WHERE group_id='3'"}
<option value='{cat_id}'>{cat_name}</option>
{/exp:query}
</select>

I would like to see the categories listing as:

Parent 1
 child 1.1
 child 1.2
Parent 2
 Child 2.1
 Child 2.2

1 Answer 1

2

The docs provide a couple of syntax options:

{category_menu}
        <label for="categories">Categories</label>
        <select name="category[]" id="categories" size="4" multiple="multiple">
                {select_options}
        </select>
{/category_menu}

Or, for finer control:

<select name="category[]" id="categories" size="4" multiple="multiple">
        {categories}
                <option value="{category_id}"{selected}>{category_name}</option>
        {/categories}
</select>

By using the {category_group_id} and {category_depth} parameters, you can refine things further. If you need to restrict it by category group, and indent things by depth (and {select_options} is inadequate), perhaps something like this would do the trick:

<select name="category[]" id="categories" size="4" multiple="multiple">
        {categories}
                {if category_group_id == 3}
                     <option value="{category_id}"{selected}>
                            {if category_depth == 2}&nbsp;&nbsp;{/if}{category_name}
                      </option>
                {/if}
        {/categories}
</select>
2
  • is there a reason why using the 2nd option above a parent category isn't auto-assigned if a child category is selected? auto-assign to parent category is set to "Yes" in channel admin global preferences
    – Tad Ward
    Commented Feb 18, 2014 at 23:09
  • Not sure @TadWard, sorry. Commented Feb 19, 2014 at 18:56

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.