2

Not sure if this can be done natively or if an add-on is out there.

I'm trying to figure out a way to limit entries with multiple parent categories to just one parent and it's children. The reason, I have nested product URL's:

http://domain.com/products/parent_cat/child_cat/entry
http://domain.com/products/parent_cat/child_cat/grand_child/entry

Currently this works well for products with a single parent category.

Right now entries with multiple parent categories build "bad" URL's in the search results with this template code:

<a href="/products/{categories}{category_url_title}/{/categories}{url_title}">{title}</a>

Which works, if the entry has only one parent category assigned to it. If it has multiple, I get a nested URL with parent and child categories mixed together:

http://domain.com/products/parent_cat/other_parent/child_parent_cat/child_other_parent_cat/entry

Any ideas on how I could limit the parent & child categories to just one parent tree? Thanks for the help!

3 Answers 3

2

I got this working correctly by using the Primary Category extension as Yuri recommended! I thought I would post my solution to help others in the future.

I created a conditional to check if the primary category is set and then built the URL based off the condition.

So if it returns true, the {primary_category_url_title} is placed outside the {categories} variable pair since it's going to be skipped inside the pair. Then I have another condition to skip all categories that don't have the same parent_id as the primary category set.

{if primary_category_id}
    <li><strong>{product_ci_model}</strong>: <a href="/products/{primary_category_url_title}/{categories}{if parent_id == primary_category_id}{category_url_title}/{/if}{/categories}{url_title}">{title}</a> - <em>Product<em></li>
{if:else}
    <li><strong>{product_ci_model}</strong>: <a href="/products/{categories}{category_url_title}/{/categories}{url_title}">{title}</a> - <em>Product<em></li>
{/if}
1

It looks like Primary Category might help you

1
  • I'll check this out.
    – Siebird
    Jan 23, 2013 at 18:49
0

You can us the limit function with {categories} pair tag. This would still allow multiple categories, parent and children, but only display one for url purposes. like

<a href="/products/{categories limit="1"}{category_url_title}/{/categories}{url_title}">{title}</a>

Otherwise, take a look at GWCode's GW Categories. It allows an insane amount of category control.

3
  • I've tried this. If I limit=2 it will output the parent categories first, then the childs: http://domain.com/products/parent_cat/other_parent_cat/entry
    – Siebird
    Jan 23, 2013 at 18:47
  • I thought you said you were looking to get 1 category. The limit should be set to 1 not 2, otherwise, you're still dealing with multiple items. Not being related back. GW Categories will possibly give you better handling. Jan 23, 2013 at 19:19
  • Sorry for the confusion. I only wanted to build the url from one parent tree. The limit parament loops through parents first, then children. I was able to do it with Primary Category and conditionals
    – Siebird
    Jan 23, 2013 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.