0

`I will try this again.

I have an index page for each category within category group 1 (markets).

I loop through the descendants of the given category to create filter buttons.

How do i pull all the categories from a 2nd category group (country) assigned to each market from markets - listing them only once

For the market from category group-1 (markets), I am using:

{% set segments = craft.categories.descendantOf(category) %} {% for category in segments %} button html using {{ category.title }} ``{% endfor %}

For the countries from category group-2 (country), I tried using:

{% set name = craft.categories.group('country').relatedTo(category) %} {% for nation in name %} button html with {{ nation.countryName }} {% endear %}

Where countryName is the field name for the country group.

I did not get a template error. But nothing printed.

1

I forgot about the related entries. So, I actually want to pull the related countries from the **entries** related to the market.

Here is the solution. The key was the answer to this related question about related tags.

{% set entries = craft.entries.relatedTo(category).find() %} {% set nation = craft.categories.group('country').relatedTo(entries).find()%}

0

Try changing

{% set name = craft.categories.group('country').relatedTo(category) %}

to

{% set name = craft.categories.group('country').relatedTo(category.slug) %}

Use the slug parameter to target a specific category in the category group. More parameters can be found here: https://craftcms.com/docs/templating/craft.categories

{% set category = craft.categories
    .group('nameOfCategoryGroup')
    .slug('specificCategoryInGroup')
    .limit(1)
    .first()
%}
  • I am still very much a novice. I literally tried it with (category.slug) and then with (category.market). I got nothing - no errors and no buttons. – Hellyar Feb 27 '17 at 20:58
  • Is the category an actual category object? I updated my answer that shows you how to get a category by its slug. – Brad Feb 28 '17 at 3:22
  • I am not trying to pull a category. I want to pull related categories from a second category group I have 2 category groups Group 1 = markets Group 2 = countries. I have an entry index for each market that appears in markets (Group 1). I then loop through each descendant of a given market to create filter buttons. Next, I want to pull the related Group 2 countries associated with the given Group 1 market' -- 1 to many. So, looking across all the entries in the index associated with the Group 1` market what countries from Group 2 are related. – Hellyar Feb 28 '17 at 19: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.