1

I have a checkbox field to enable my client to choose what entry categories to have as an article feed on a page. How can I output the checked option categories when setting the entries in the for loop? Below is what I have done to return the checked option's value but what I want to do is use the checked options value to filter through entry categories.

What I'm looking to do is set the entries as below:

{% set entries = craft.entries.relatedTo(category, 'checkedOptionValue','checkedOptionValue','checkedOptionValue').limit(10).find() %}

<!-- This is returning the checked options value -->
{% for option in checkboxFieldHandle.options %}
    {% if option.selected %}
        {{ option }}
    {% endfor %}
{% endfor %}

Any help would be greatly appreciated

2

The most straightforward way would be to use a Categories field.

The value that a Categories field returns to the template is an ElementCriteriaModel, which fits perfectly as a value for the .relatedTo parameter.

So you could do something like:

{% set featuredEntries = craft.entries.relatedTo( entry.selectedCategories ) %}

The only circumstance where I can envision a Checkboxes field being better suited to this use-case is if you need to only provide a specific subset of the categories in a group.

(If you want to make all the categories from a group available for selection, using a Categories field is definitely best; otherwise you have to go back and update the options of your Checkboxes field every time a category is created, changed, or deleted.)

In that case, I recommend using the category IDs as the option values for your checkboxes, so that you can pass them to an ElementCriteriaModel like this:

{% set featuredEntries = craft.entries.relatedTo( entry.selectedCheckboxes | join ) %}
|improve this answer|||||
  • Using the Categories field was definitely the best way to go about it, thanks for your help. No idea why I ruled out using the Categories field in the first place. – ChocolateOrange Sep 3 '16 at 17:14

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.