<p>This can be accomplished using Craft's <a href="http://buildwithcraft.com/docs/relations" rel="nofollow">Relations</a>.</p>

<p><em>My example code assumes you know the blog categories slug, and that you are pulling posts from a single category.</em></p>

<p>First, you'll have to get a reference to the <a href="http://buildwithcraft.com/docs/templating/craft.categories" rel="nofollow">category ElementCriteriaModel</a> object pointing to the category of the blog post.</p>

<pre><code>{% set category = craft.categories.slug('your-category-slug').first() %}
</code></pre>

<p>With a reference to the category model, you'll be able to find related entries.</p>

<pre><code>{% for post in craft.entries.section('blog').relatedTo({targetElement: category}) %}
    {% if post.id != entry.id %}
        &lt;div class="related-entry"&gt;
            &lt;h2&gt;{{ post.title }}&lt;/h2&gt;
            {{ post.body }}
        &lt;/div&gt;
    {% endif %}
{% endfor %}
</code></pre>

<p>The if statement makes sure you aren't including the current post with the related posts.</p>

CC BY-SA 3.0
2014-06-26T10:42:43.843Z
It is also probably worth throwing an if statement in the loop so you don't display the original entry as a related entry.
Andris Sevcenko
CC BY-SA 3.0
2014-06-26T16:53:03.953Z
Yeah, great call. I included that in the answer.
Coppa
CC BY-SA 3.0
2014-06-26T06:42:54.967Z
2014-06-26T16:52:14.787Z
2014-06-26T16:52:14.787Z
Coppa
<p>This can be done quite easily as follows, assuming that you are on an entry page so <code>entry</code> is a variable in your template and your category field is called <code>categories</code>.</p>

<pre class="lang-twig prettyprint-override"><code>{% for relatedEntry in craft.entries({
    section: 'blog',
    relatedTo: entry.categories,
    id: 'not ' ~ entry.id
}) %}
    &lt;a href="{{ relatedEntry.url }}"&gt;{{ relatedEntry.title }}&lt;/a&gt;
{% endfor %}
</code></pre>

<p>I am passing an object into the ElementCriteriaModel for clarity above but you could just as easily do it by chaining parameters onto the ElementCriteriaModel as follows, it's just a matter of preference.</p>

<pre class="lang-twig prettyprint-override"><code>{% for relatedEntry in craft.entries.section('blog').relatedTo(entry.categories).id('not ' ~ entry.id) %}
    &lt;a href="{{ relatedEntry.url }}"&gt;{{ relatedEntry.title }}&lt;/a&gt;
{% endfor %}
</code></pre>

<p>See <a href="http://buildwithcraft.com/docs/relations#the-relatedTo-param" rel="nofollow">the relatedTo param</a> in the docs to understand the magic.</p>

CC BY-SA 3.0
2014-06-26T23:47:34.677Z
Thanks guys! :) Would be awesome and very useful if someone could add the categories & tags templates on the Craft demo site github.com/pixelandtonic/ontherocks ;)
lucassoares
CC BY-SA 3.0
2014-06-27T01:13:49.063Z
@lucassoares Sounds like somebody's got a great idea for a pull request! And by somebody, I mean you. :)
Matt Stein
CC BY-SA 3.0
2014-06-26T20:33:45.517Z
2014-06-27T16:25:56.220Z
2014-06-27T16:25:56.220Z
Ben Croker
<pre><code>{% set category = craft.categories.group('{{ entry.topic }}').first() %}`enter code here`
</code></pre>

<p>For anyone else who stumbles on this post, I wasn't able to get the code working until I changed a few things.</p>

<p>Instead of categories.<em>slug</em>, I used category.<em>group</em> and made 'your category-slug' {{ entry.topic }}, topic being how I assign my <em>single</em> category to entries.</p>

CC BY-SA 3.0
2016-04-10T18:20:09.750Z
Passing a string `'{{ entry.topic }}'` to the param works for you? Doesn't sound right...
carlcs
CC BY-SA 3.0
2016-04-11T22:20:07.007Z
topic is how I call the single category name that I've assigned to my entries.
Aaron Ransom
CC BY-SA 3.0
2016-04-10T14:35:28.917Z
2016-04-10T14:35:28.917Z
Aaron Ransom
<p>I'm converting a WordPress blog to Craft - first time playing with categories in Craft, so how can I display the related posts (by category) at the bottom of each post? </p>

<entries><categories><relations>
How can I display the related posts (by category) at the bottom of each post?
# How can I display the related posts (by category) at the bottom of each post?

(Asked by: username_0 on 2014-06-26)

I'm converting a WordPress blog to Craft - first time playing with categories in Craft, so how can I display the related posts (by category) at the bottom of each post?

**Tags:** entries, categories, relations

---

## 3 answers

### Answer by username_1 on 2014-06-26. Score: 4

This can be accomplished using Craft's Relations.
My example code assumes you know the blog categories slug, and that you are pulling posts from a single category.
First, you'll have to get a reference to the category ElementCriteriaModel object pointing to the category of the blog post.
{% set category = craft.categories.slug('your-category-slug').first() %}

With a reference to the category model, you'll be able to find related entries.
{% for post in craft.entries.section('blog').relatedTo({targetElement: category}) %}
    {% if post.id != entry.id %}
        
            {{ post.title }}
            {{ post.body }}
        
    {% endif %}
{% endfor %}

The if statement makes sure you aren't including the current post with the related posts.

> **Comments:**
> **username_2**: It is also probably worth throwing an if statement in the loop so you don't display the original entry as a related entry.  
> **username_1**: Yeah, great call. I included that in the answer.  

---

### Answer by username_3 on 2014-06-26. Score: 4

This can be done quite easily as follows, assuming that you are on an entry page so entry is a variable in your template and your category field is called categories.
{% for relatedEntry in craft.entries({
    section: 'blog',
    relatedTo: entry.categories,
    id: 'not ' ~ entry.id
}) %}
    {{ relatedEntry.title }}
{% endfor %}

I am passing an object into the ElementCriteriaModel for clarity above but you could just as easily do it by chaining parameters onto the ElementCriteriaModel as follows, it's just a matter of preference.
{% for relatedEntry in craft.entries.section('blog').relatedTo(entry.categories).id('not ' ~ entry.id) %}
    {{ relatedEntry.title }}
{% endfor %}

See the relatedTo param in the docs to understand the magic.

> **Comments:**
> **username_0**: Thanks guys! :) Would be awesome and very useful if someone could add the categories & tags templates on the Craft demo site github.com/pixelandtonic/ontherocks ;)  
> **username_4**: @lucassoares Sounds like somebody's got a great idea for a pull request! And by somebody, I mean you. :)  

---

### Answer by username_5 on 2016-04-10. Score: 1

{% set category = craft.categories.group('{{ entry.topic }}').first() %}`enter code here`

For anyone else who stumbles on this post, I wasn't able to get the code working until I changed a few things.
Instead of categories.slug, I used category.group and made 'your category-slug' {{ entry.topic }}, topic being how I assign my single category to entries.

> **Comments:**
> **username_6**: Passing a string `'{{ entry.topic }}'` to the param works for you? Doesn't sound right...  
> **username_5**: topic is how I call the single category name that I've assigned to my entries.  

---

