3

What we are trying to do:
I am using three category-groups, "product category", brands and country of origin, to create a navigation in a shop with the ability to filter the content of each product category with a specific brand or country.

The product category group is used to generate the main navigation and when a category is selected a listing-page is displayed with the entries of that category.

The display page in turn shows a "sub-nav" of the different brands and countries of origins.

Structure of the product category group:

Main category 1
    sub-category 1
    sub-category 2
Main category 2
    sub-category 3

When i pick a sub-category my url looks like this:

domain.com/products/[sub-category]

This page should show all products in this category and possible 3rd levels of product groups in the top and then brands and countries in a sub-nav.

It should look a little like this:

3rd lvl sub-category
-
Brands
    Brand 2
    Brand 5
Countries of origin
    Country 1
    Country 3

Where Brand 1/3/4 and Country 2 are not shown since no product with the [sub-category] has them selected.

When i select a brand or country that segment is added to the url and {segment_category_ids} gives me all the matched categories for use in the {exp:channel:entries} tag.

This is where i am having some problems..
I cant get my head around how to display only the brands and countries that have entries in the already selected [sub-category].

By default it displays every category that is used by any entry in the products channel. We haven't used categories all that much so i am not sure that i am going at it the right way.

I have tried to create a list of all the entry_id's from the subset and use those pipe-separated in the show-parameter of the {exp:channel:categories} tag-pair but i haven't gotten it to work yet.

We have mostly been using Playa for relations between different entries and "pages" before but i thought i could be good to get a grip on how to do it natively.

Does anyone have a better approach or any ideas on what i should do?

  • Just for clarity: are "brands" and "countries of origin" channels or category groups? – Tom Davies Nov 28 '12 at 14:29
  • Correct, 3 groups (product category, brands and country of origin) – Kim Wickell Nov 28 '12 at 14:42
2

I don't think you can achieve this result with pure EE,but if you don't mind using some php, you could do something along these lines:

This runs through you entries ( it's a dynamic ee tag, so if your product category is defined in the url, it should only show the entries in that category ) and adds their categories to an array in PHP.

<?php $categories = array(); $i = 0; ?>
{exp:channel:entries channel="products"}
    {categories}
       <?php
           if(!in_array("{category_url_title}", $categories)){
               $categories[$i][0] = "{category_id}";
               $categories[$i][1] = "{category_name}";
               $categories[$i][2] = "{category_url_title}";
               $i++;
           }
       ?>
    {/categories}
{/exp:channel:entries}

You can use this PHP array to output the categories. This array should only contain the categories that have entries which are in the product-category of the page you're on.

It might very well be that there's a much easier, and especially cleaner, way to achieve this result though.. but it should work. :)

| improve this answer | |
  • I used your solution with a few changes, i added {category_group} to the array to be able to list each of my groups seperately. Will continue to tinker with it but this will do nicely for now. – Kim Wickell Nov 28 '12 at 15:10
  • great, could you mark this as answered ? – janvl Nov 28 '12 at 15:13
2

We handle this by placing the categories in the URL as segments (example.com/store/category_foo/category_bar), and use the seg2cat add-on, which will give you a {segment_category_ids} variable, which can be used like this:

{exp:channel:entries category="{segment_category_ids}" dynamic="no"}

This will give you the ability to handle multiple category id's in the URL, which I believe might be part of the problem you're trying to solve.

| improve this answer | |
0

So my approach to this sort of thing is to use Low's seg2cat extension. As the name suggests, this lets you get from a url segment to a category_id/name etc.

Hence if your URL is http://example.com/fruit/apples, then {segment_2_category_id} will be the category_id of your "fruit" category.

You can then push this into your channel:entries tag:

{exp:channel:entries
  channel="brands"
  category="{segment_2_category_id}"
  dynamic="no"
}
  <li>{title}</li>
{/exp:channel:entries}

Extra tip: If you need this to work with a fallback category (when no cat in the URL) you can take advantage of the parse order and the way preload_replace works to do something like this:

{if segment_2_category_id}
  {preload_replace:pre_current_category="{segment_2_category_id}"}
{/if}
{preload_replace:pre_current_category="n"}

{exp:channel:entries
  channel="brands"
  category="{pre_current_cat}"
  dynamic="no"
}
  <li>{title}</li>
{/exp:channel:entries}

As attempts to redefine a preload_replace are ignored and the first value used, and simple conditionals are parsed before preload_replaces.

| improve this answer | |
  • We are using Low's seg2cat to get the id's but the problem isn't to get the category ids from the url. I cant get the brand and country category to display the categories used in the subset that i get from filtering my "product_categories" category. – Kim Wickell Nov 28 '12 at 14:15
  • ah okay, so you want to filter categories by those that have entries in the category in the URL? Perhaps you could add some (pseudo) code samples to your qn to make it a little clearer, looks like I an a few others got the wrong end of the stick – Tom Davies Nov 28 '12 at 14:21
  • Updated the explanation, hope its clearer now. – Kim Wickell Nov 28 '12 at 14:38
0

It looks like you've got your navigation working how you want but it's displaying categories and sub-categories that's the problem? GWCode Categories Addon: http://gwcode.com/add-ons/gwcode-categories is excellent for going through a hierarchy of categories.

I'm struggling a bit with your question. With your actual category groups and an example layout I'd be able to offer some code as well.

| improve this answer | |
  • I updated the question a bit ago if that helps. Got it working with @janvl's tip of using php to get the categories. Is there anything else i could put up there? – Kim Wickell Nov 28 '12 at 15:25
  • I'm averse to turning on PHP if I don't have to. I'm sure GWCode Categories Addon can probably output what you need. But if you're happy with Janvi's solution, that's great. – Elliot Lewis Nov 28 '12 at 16:44
  • I needed to get it up as quickly as possible so mixing-in php works until i can get a better solution in place. I will take a look at GWCode Categories, i could probably get it to work using the entry_id-parameter and get enter all of the entries from the sub-category in there. Thanks for the tip! – Kim Wickell Nov 28 '12 at 16:52
0

Kim,

Can you share your PHP solution with me that apparently solved your issue> I'm running into a similar situation with multiple categories to filter on. I'm a PHP newbe....

| improve this answer | |

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.