<p>As I understand it,  you want the url for an entry to be <code>/section/category-slug/entry-slug</code>. This is a little tricky, because if the entry has no category defined, it won't have a url, which is why you can't just set the <em>Entry URL Format</em>.</p>

<p>If you really want to do this, you can do it using routes rather than the entry URL format. Uncheck the <em>Entries in this section have their own URLs</em> box, and create a route for section/[slug]/[slug] that points to your entry template.</p>

<p>On the entry template, you can figure out the category and entry slugs:</p>

<pre><code>{% set categorySlug = craft.request.getSegment(2) %}
{% set entrySlug = craft.request.getSegment(3) %}
</code></pre>

<p>get the category:</p>

<pre><code>{% set category = craft.category.slug(categorySlug) %}
{% if not category %}{% exit 404 %}{% endif %}
</code></pre>

<p>get the entry:</p>

<pre><code>{% set entry = craft.entries.slug(entrySlug).relatedTo(category).first %}
{% if not entry %}{% exit 404 %}{% endif %}
</code></pre>

<p>Now you have the entry, proceed as usual.</p>

<p>When you need the entry url, that will be:</p>

<pre><code>/section/{{ entry.category.first.slug }}/{{ entry.slug }}
</code></pre>

<p>I would write a macro for the entry url, and the macro can deal with the possibility of the entry having no category.</p>

CC BY-SA 3.0
2014-07-21T00:20:54.770Z
Right, I have all of that already.  My question is related to URL returned in the search results. Under the section's settings config page where you check the "Entries in this section have their own URLs" box, for the "Entry URL Format" how do I specify that the category slug should be part of the URL format?  Even if set the URL format to "section/{{ entry.category.first.slug }}/{slug}" the search results only returns "section//entry-slug" and drops the category slug from the URL.
Steve Adams
CC BY-SA 3.0
2014-07-20T22:36:29.023Z
2014-07-20T22:36:29.023Z
Marion Newlevant
<p>This got it working for me:</p>

<pre><code>/section/{categoryFieldName.first.slug}/{slug}
</code></pre>

CC BY-SA 3.0
2014-07-21T00:35:22.693Z
2014-07-21T00:35:22.693Z
Steve Adams
<p>I have a custom section that uses routes like this:</p>

<p>/section/category/entry_url</p>

<p>In trying to program the search form results I realized that I need to explicitly define the entry URL format for the section so that the search results return the proper links to view the entries.</p>

<p>Currently I have <code>/section/{category.slug}/{slug}</code> but it is not working.</p>

<p>How would I set the entry URL format to include only the first category slug of the entry?</p>

<p>Thanks.</p>

<url><search><entrymodel><slug><category>
How would I include the first category slug in the section's entry URL format?
# How would I include the first category slug in the section's entry URL format?

(Asked by: username_0 on 2014-07-20)

I have a custom section that uses routes like this:

/section/category/entry\_url

In trying to program the search form results I realized that I need to explicitly define the entry URL format for the section so that the search results return the proper links to view the entries.

Currently I have `/section/{category.slug}/{slug}` but it is not working.

How would I set the entry URL format to include only the first category slug of the entry?

Thanks.

**Tags:** url, search, entrymodel, slug, category


---

## 2 answers

### Answer by username_0 on 2014-07-21. Score: 16 | Accepted answer

This got it working for me:
/section/{categoryFieldName.first.slug}/{slug}


---

### Answer by username_1 on 2014-07-20. Score: 4

As I understand it,  you want the url for an entry to be /section/category-slug/entry-slug. This is a little tricky, because if the entry has no category defined, it won't have a url, which is why you can't just set the Entry URL Format.
If you really want to do this, you can do it using routes rather than the entry URL format. Uncheck the Entries in this section have their own URLs box, and create a route for section/[slug]/[slug] that points to your entry template.
On the entry template, you can figure out the category and entry slugs:
{% set categorySlug = craft.request.getSegment(2) %}
{% set entrySlug = craft.request.getSegment(3) %}

get the category:
{% set category = craft.category.slug(categorySlug) %}
{% if not category %}{% exit 404 %}{% endif %}

get the entry:
{% set entry = craft.entries.slug(entrySlug).relatedTo(category).first %}
{% if not entry %}{% exit 404 %}{% endif %}

Now you have the entry, proceed as usual.
When you need the entry url, that will be:
/section/{{ entry.category.first.slug }}/{{ entry.slug }}

I would write a macro for the entry url, and the macro can deal with the possibility of the entry having no category.

> **Comments:**
> **username_0**: Right, I have all of that already.  My question is related to URL returned in the search results. Under the section's settings config page where you check the "Entries in this section have their own URLs" box, for the "Entry URL Format" how do I specify that the category slug should be part of the URL format?  Even if set the URL format to "section/{{ entry.category.first.slug }}/{slug}" the search results only returns "section//entry-slug" and drops the category slug from the URL.  

---

