1

I'm using a category group and routes to serve up entries on landing pages with a URL structure like this.

www.domain.com/properties/retail/freestanding

My code works, but I am wondering if there's a better way to write it (dry it up).

{% set allProperties = craft.entries.section('propertiesSection') %}
{% set cat = craft.request.getSegment(2) %}
{% set subcat = craft.request.lastSegment %}

{% set retail = craft.categories.group('properties').craft.categories.slug('retail-properties') %}

{% if subcat == "freestanding" %}
    {% set entries = allProperties.relatedTo([ {targetElement: freestanding } ]) %}
{% endif %}

{% if subcat == "storefront" %}
    {% set entries = allProperties.relatedTo([ {targetElement: storefront } ]) %}
{% endif %}

.... and so on

{% if entries|length %}
    {{ entry.title }}
{% endif %}
  • Your code seems a little bit confusing to me. Could you please explain the reason why you did what you did and explain what it is supposed to do? I'm not sure if you are trying to search a work around for default features or if you don't know them or if I can't understand your code. Just one note craft.categories.group('properties').craft.categories.slug('retail-properties') does not make sense. You can remove the first part and use only craft.categories.slug('retail-properties') but even so fetching entries by slug is not idea. As soon as a redactor changes that value your logic breaks – Robin Schambach Feb 14 '18 at 14:29
  • Thanks, I didn't realize that I could only cite the category slug. In my case my categories are set so the slugs will not be changing. But I am open to a better way of doing things. Essentially what I'm doing is displaying entries assigned to a parent category and giving the user the ability to refine them by sub-category. I am new to this so your input is greatly appreciated. – triplethreat23 Feb 14 '18 at 18:42
1

If you category is related to your entry there is no need to fetch the category via url segment. This method is slower and will need more resources since you'll do the work twice for no reason.

entry.relationFieldHandle

will contain an ElementCriteriaModel with a ready to use query to fetch the related elements. So entry.relationFieldHandle.first() will return the category.

If you want to get all entries, that are related to the category of your current entry you should do

{% set categoryIds = entry.fieldHandle.ids() %}
{% set relatedEntries = craft.entries.relatedTo(categoryIds).find() %}

Your code could be

{% set category = entry.fieldHandle.first() %}
{% set relatedEntries = craft.entries.section('propertiesSection').relatedTo(category).find() %}

That's basically it. Note: you don't need to fetch the category to get the related entries, you only need the id. That's why I used .ids() in the first example instead of .first() or .find()

| improve this answer | |
  • The problem is I do not have an entry yet. I'm using this to create a landing page to present the entries in a category/sub-category to the user. – triplethreat23 Feb 15 '18 at 15:11

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.