<p>/work/ecommerce-websites is giving you a 404 because there is no obvious place to route it. See <a href="http://buildwithcraft.com/docs/routing" rel="nofollow noreferrer">http://buildwithcraft.com/docs/routing</a> for details on how Craft tries to route each request that comes in.</p>

<p>You have 3 options:</p>

<p><strong>GOOD: Create individual templates for each category</strong></p>

<p>I’m not sure why you would want to do that, but it <em>is</em> an option, so I’m putting it here.</p>

<p><strong>BETTER: Create a dynamic route</strong></p>

<p>You can create a new <a href="http://buildwithcraft.com/docs/routing#dynamic-routes" rel="nofollow noreferrer">dynamic route</a> that tells Craft to point URLs that look something like “work/(slug)” to the “work” template, or you might be better off just creating a new “work/_category” template so that you don’t have a single template that is responsible for two entirely different types of pages, which is a little weird. (If there are similarities between the work index and a work category page, you should probably just create a new Work-specific layout template that outputs those things.)</p>

<p><strong>BEST: Make these your primary category URLs</strong></p>

<p>You said you have a category group set up, and the second segment is supposed to match one of them. If this is the <strong>primary</strong> place that you would expect to find info about these categories on your site, then go into Settings > Categories > [your group] and check the “Categories in this group have their own URLs” checkbox, and fill in the “Category URL Formats” and “Category Template” settings like so:</p>

<p><img src="https://i.sstatic.net/44LHe.png" width="617" alt="Craft category group settings"></p>

<p>Then, double-click on each of your categories and make sure their Slugs are what you would expect the 2nd segments to be.</p>

<p>Here I’m expecting you’ve decided to create a “work/_category” template as suggested earlier, but if you really want your work index template to be handling these pages you can change the Category Template setting to just plain “work”.</p>

<p>As a bonus with this third approach, you won’t need to fetch your categories manually from the template; a <code>category</code> variable will already be available to the template, so you can go straight to outputting <code>{{ category.title }}</code> or whatever. (See step 4 on <a href="http://buildwithcraft.com/docs/routing" rel="nofollow noreferrer">http://buildwithcraft.com/docs/routing</a>).</p>

<p>This third technique also applies to sections/entries by the way. I mention that because based on your posted template code (<code>{{ entry.websiteLink }}</code>) it looks like you’re managing entry URLs manually with a custom field, which is really not necessary in Craft. Check the “Entries in this section have their own URLs” checkbox in your section’s settings and Craft will assign a primary URL to all the entries in your section, based on the URL Format you give it.</p>

CC BY-SA 3.0
2014-09-11T09:11:35.547Z
Awesome, thanks Brandon!
xenowebdev
CC BY-SA 3.0
2014-09-11T08:46:09.993Z
2014-09-11T08:46:09.993Z
Brandon Kelly
<p>After a few more hours of reading and trying, I have now come up with the following solution for displaying a landing page and filtered entries by segment() in the same template:
(as a newbie I don't know if this is "best practise", maybe someone could clarify that?)</p>

<pre><code>{% switch craft.request.getSegment(2) %}
{% case "" %}

{% for entry in craft.entries.section('portfolioImages').order('id desc') %}
&lt;div class="col-sm-4 col-md-4 col-lg-3 work-item"&gt; 
    &lt;div class="holder2"&gt;
        &lt;div class="portfolio-item2"&gt;
            &lt;a href="{{ entry.websiteLink }}" target="_blank" title="View site"&gt;
            {% for image in entry.image %}
                    &lt;img src="{{ image.url() }}" alt="{{ entry.alternativeText }}" class="img-responsive center-block" /&gt;
            {% endfor %}
            &lt;/a&gt;
            &lt;div class="client-name"&gt;
                &lt;p&gt;{{ entry.title }}&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
{% endfor %}

{% default %}

{% for category in craft.categories.slug(craft.request.getSegment(2)).find() %}
    {% for entry in craft.entries.section('portfolioImages').relatedTo({targetElement: category}).find() %}
        &lt;div class="col-sm-4 col-md-4 col-lg-3 work-item"&gt; 
            &lt;div class="holder2"&gt;
                &lt;div class="portfolio-item2"&gt;
                    &lt;a href="{{ item.websiteLink }}" target="_blank" title="View site"&gt;
                    {% for image in entry.image %}
                            &lt;img src="{{ image.url() }}" alt="{{ entry.alternativeText }}" class="img-responsive center-block" /&gt;
                    {% endfor %}
                    &lt;/a&gt;
                    &lt;div class="client-name"&gt;
                        &lt;p&gt;{{ entry.title }}&lt;/p&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    {% endfor %}
{% endfor %}﻿

{% endswitch %}
</code></pre>

CC BY-SA 3.0
2014-09-11T08:34:35.523Z
2014-09-11T08:34:35.523Z
xenowebdev
<p>To sum it up, you want a single template to</p>

<ul>
<li>display all "portfolioImages" items if no category is selected (appears in the URL)</li>
<li>display only "portfolioImages" items belonging to a category if one category is selected (appears in the URL)</li>
</ul>

<p>The key is to use categories having their own URLs (in the CP for your category group) and the category variable Craft creates for you automatically if the URL calls for a category page. You can use that variable to specify that you only want entries related to that current category to display.</p>

<p>I wrote a <a href="http://craftcookbook.net/recipes/102" rel="nofollow">recipe in Craft Cookbook a while back</a>. Have a look at the second version entitled "with native categories".</p>

CC BY-SA 3.0
2014-09-11T11:15:33.457Z
No problem. Hope it will help you. The approach is a bit different than with EE but it makes a lot of sense
Jérôme Coupé
CC BY-SA 3.0
2014-09-11T09:09:14.983Z
2014-09-11T09:09:14.983Z
Jérôme Coupé
<p>Sorry if there is an answer out there for this, I've read quite a few posts but have got myself confused. I hope you can point me in the right direction.</p>

<p>I have this code in my EE template:</p>

<pre><code>{exp:switchee variable="{segment_2}" parse="inward"}
{case value="''"}
{exp:channel:entries channel="portfolio" dynamic="no" status="open" disable="pagination|member_data" orderby="date" sort="desc"}
&lt;div class="col-sm-4 col-md-4 col-lg-3"&gt; 

    {cf_portfolio_link}
    {cf_portfolio_image}
    {title}

&lt;/div&gt;
{/exp:channel:entries}
{/case}
{case default="yes"}
{exp:channel:entries channel="portfolio" dynamic="no" category="{segment_2_category_id}" status="open" disable="pagination|member_data" orderby="date" sort="desc"}
&lt;div class="col-sm-4 col-md-4 col-lg-3"&gt; 

    {cf_portfolio_link}
    {cf_portfolio_image}
    {title}

&lt;/div&gt;
{/exp:channel:entries}
{/case}
{/exp:switchee}
</code></pre>

<p>I'd like to do the same in Craft - use one template and filter my entries by segment(2).</p>

<p>At present I have this code in my Craft template (the first case works, the second returns a 404):</p>

<pre><code>{% switch craft.request.getSegment(2) %}
{% case "" %}

{% for item in craft.entries.section('portfolioImages').order('id desc') %}
    &lt;div class="col-sm-4 col-md-4 col-lg-3 work-item"&gt; 
    &lt;a href="{{ item.websiteLink }}" target="_blank" title="View site"&gt;
    {% for image in item.image %}
        &lt;img src="{{ image.url() }}" alt="{{ item.alternativeText }}" class="img-responsive center-block" /&gt;
    {% endfor %}
    &lt;/a&gt;
    &lt;p&gt;{{ item.title }}&lt;/p&gt;
    &lt;/div&gt;
{% endfor %}

{% default %}

    filtered entries to go here

{% endswitch %}
</code></pre>

<p>The URL for my portfolio page is mysite.com/work or mysite.com/work/ecommerce-websites if it has been filtered.</p>

<p>I do have a category group set up in the system called Portfolio with 3 top level categories only:</p>

<ul>
<li>ecommerce-websites </li>
<li>promotion-websites</li>
<li>product-catalogue-websites</li>
</ul>

<p>Not sure where to go from here now.</p>

<p>Let me know if you need further information.</p>

<p>Cheers</p>

<entries><categories>
Porting from EE - How to filter by category/segment in one template
# Porting from EE - How to filter by category/segment in one template

(Asked by: username_0 on 2014-09-11)

Sorry if there is an answer out there for this, I've read quite a few posts but have got myself confused. I hope you can point me in the right direction.
I have this code in my EE template:
{exp:switchee variable="{segment_2}" parse="inward"}
{case value="''"}
{exp:channel:entries channel="portfolio" dynamic="no" status="open" disable="pagination|member_data" orderby="date" sort="desc"}
 

    {cf_portfolio_link}
    {cf_portfolio_image}
    {title}


{/exp:channel:entries}
{/case}
{case default="yes"}
{exp:channel:entries channel="portfolio" dynamic="no" category="{segment_2_category_id}" status="open" disable="pagination|member_data" orderby="date" sort="desc"}
 

    {cf_portfolio_link}
    {cf_portfolio_image}
    {title}


{/exp:channel:entries}
{/case}
{/exp:switchee}

I'd like to do the same in Craft - use one template and filter my entries by segment(2).
At present I have this code in my Craft template (the first case works, the second returns a 404):
{% switch craft.request.getSegment(2) %}
{% case "" %}

{% for item in craft.entries.section('portfolioImages').order('id desc') %}
     
    
    {% for image in item.image %}
        
    {% endfor %}
    
    {{ item.title }}
    
{% endfor %}

{% default %}

    filtered entries to go here

{% endswitch %}

The URL for my portfolio page is mysite.com/work or mysite.com/work/ecommerce-websites if it has been filtered.
I do have a category group set up in the system called Portfolio with 3 top level categories only:

ecommerce-websites 
promotion-websites
product-catalogue-websites

Not sure where to go from here now.
Let me know if you need further information.
Cheers

**Tags:** entries, categories

---

## 3 answers

### Answer by username_1 on 2014-09-11. Score: 5 | Accepted answer

/work/ecommerce-websites is giving you a 404 because there is no obvious place to route it. See <http://buildwithcraft.com/docs/routing> for details on how Craft tries to route each request that comes in.

You have 3 options:

**GOOD: Create individual templates for each category**

I’m not sure why you would want to do that, but it *is* an option, so I’m putting it here.

**BETTER: Create a dynamic route**

You can create a new [dynamic route](http://buildwithcraft.com/docs/routing#dynamic-routes) that tells Craft to point URLs that look something like “work/(slug)” to the “work” template, or you might be better off just creating a new “work/\_category” template so that you don’t have a single template that is responsible for two entirely different types of pages, which is a little weird. (If there are similarities between the work index and a work category page, you should probably just create a new Work-specific layout template that outputs those things.)

**BEST: Make these your primary category URLs**

You said you have a category group set up, and the second segment is supposed to match one of them. If this is the **primary** place that you would expect to find info about these categories on your site, then go into Settings > Categories > [your group] and check the “Categories in this group have their own URLs” checkbox, and fill in the “Category URL Formats” and “Category Template” settings like so:

![Craft category group settings](https://i.sstatic.net/44LHe.png)

Then, double-click on each of your categories and make sure their Slugs are what you would expect the 2nd segments to be.

Here I’m expecting you’ve decided to create a “work/\_category” template as suggested earlier, but if you really want your work index template to be handling these pages you can change the Category Template setting to just plain “work”.

As a bonus with this third approach, you won’t need to fetch your categories manually from the template; a `category` variable will already be available to the template, so you can go straight to outputting `{{ category.title }}` or whatever. (See step 4 on <http://buildwithcraft.com/docs/routing>).

This third technique also applies to sections/entries by the way. I mention that because based on your posted template code (`{{ entry.websiteLink }}`) it looks like you’re managing entry URLs manually with a custom field, which is really not necessary in Craft. Check the “Entries in this section have their own URLs” checkbox in your section’s settings and Craft will assign a primary URL to all the entries in your section, based on the URL Format you give it.

> **Comments:**
> **username_0**: Awesome, thanks Brandon!  

---

### Answer by username_0 on 2014-09-11. Score: 1

After a few more hours of reading and trying, I have now come up with the following solution for displaying a landing page and filtered entries by segment() in the same template:
(as a newbie I don't know if this is "best practise", maybe someone could clarify that?)
{% switch craft.request.getSegment(2) %}
{% case "" %}

{% for entry in craft.entries.section('portfolioImages').order('id desc') %}
 
    
        
            
            {% for image in entry.image %}
                    
            {% endfor %}
            
            
                {{ entry.title }}
            
        
    

{% endfor %}

{% default %}

{% for category in craft.categories.slug(craft.request.getSegment(2)).find() %}
    {% for entry in craft.entries.section('portfolioImages').relatedTo({targetElement: category}).find() %}
         
            
                
                    
                    {% for image in entry.image %}
                            
                    {% endfor %}
                    
                    
                        {{ entry.title }}
                    
                
            
        
    {% endfor %}
{% endfor %}﻿

{% endswitch %}


---

### Answer by username_2 on 2014-09-11. Score: 1

To sum it up, you want a single template to

* display all "portfolioImages" items if no category is selected (appears in the URL)
* display only "portfolioImages" items belonging to a category if one category is selected (appears in the URL)

The key is to use categories having their own URLs (in the CP for your category group) and the category variable Craft creates for you automatically if the URL calls for a category page. You can use that variable to specify that you only want entries related to that current category to display.

I wrote a [recipe in Craft Cookbook a while back](http://craftcookbook.net/recipes/102). Have a look at the second version entitled "with native categories".

> **Comments:**
> **username_2**: No problem. Hope it will help you. The approach is a bit different than with EE but it makes a lot of sense  

---

