<p>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.</p>

<pre><code>entry.relationFieldHandle
</code></pre>

<p>will contain an <a href="https://craftcms.com/docs/templating/elementcriteriamodel" rel="nofollow noreferrer">ElementCriteriaModel</a> with a ready to use query to fetch the related elements. So <code>entry.relationFieldHandle.first()</code> will return the category. </p>

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

<pre><code>{% set categoryIds = entry.fieldHandle.ids() %}
{% set relatedEntries = craft.entries.relatedTo(categoryIds).find() %}
</code></pre>

<p>Your code could be</p>

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

<p>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 <code>.ids()</code> in the first example instead of <code>.first()</code> or <code>.find()</code></p>

CC BY-SA 3.0
2018-02-15T15:11:26.333Z
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
CC BY-SA 3.0
2018-02-14T21:47:37.370Z
2018-02-14T21:47:37.370Z
Robin Schambach
<p>I'm using a category group and routes to serve up entries on landing pages with a URL structure like this.</p>

<pre><code>www.domain.com/properties/retail/freestanding
</code></pre>

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

<pre><code>{% 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 %}
</code></pre>

<p>.... and so on</p>

<pre><code>{% if entries|length %}
    {{ entry.title }}
{% endif %}
</code></pre>

CC BY-SA 3.0
2018-02-14T14:29:03.247Z
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
CC BY-SA 3.0
2018-02-14T18:42:59.267Z
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
<category><slug><segments><getsegment>
Categories Structure for Landing Pages
# Categories Structure for Landing Pages

(Asked by: username_0 on 2018-02-13)

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 %}

**Tags:** category, slug, segments, getsegment
> **Comments:**
> **username_1**: 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  
> **username_0**: 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.  

---

## 1 answers

### Answer by username_1 on 2018-02-14. Score: 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()

> **Comments:**
> **username_0**: 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.  

---

