Categories Structure for Landing Pages 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 %}
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()