1

I have a products section as follows with a global header that outputs category fields into the meta info and page content:

domain.com/products/ (All top level product categories listed) Template= /products/index

domain.com/products/category/entries/ (Lists the products within current category) Template= /products/entries

domain.com/products/category/list/ (Lists the subcategories for any top level categories that have them) Template= /products/list

domain.com/products/detail (Shows the entry for the product) Template= /products/detail

I'd like to combine index, entries and list into 1 template at index and am trying to do by using a conditional:

{if segment_2==""} ...shows all top level product categories (This works) {if:else} ...show products within current category specified by segment 2 of URL (can't get this to work) {/if}

| improve this question | | | | |
2

Within the products/index template:

{if segment_2 == ""}
    Display all top level product categories
    CURRENT TEMPLATE IS /products/index - so would embed another template?
{if:elseif segment_3 == "entries"}
    Lists the products within current category
    {embed=products/entries}
{if:elseif segment_3 == "list"}
    Lists the subcategories for any top level categories
    {embed=products/list}
{if:elseif segment_2 == "detail"}
    Shows the entry for the product
    {embed=products/detail}
{if:else}
    404!
{/if}

But, I would recommend that /products/details/XXX is an ugly URL for the customer and for SEO purposes. You can actually do away with this and assume the product url_title directly at segment_2, then rely on the {if no_results} tag to capture the 404. So from the above example, replace this:

{if:elseif segment_2 == "detail"}
    Shows the entry for the product
    {embed=products/detail}
{if:else}
    404!
{/if}

...with just this:

{if:else}
    Shows the entry for the product
    {embed=products/detail}
{/if}

...and then in the products/details template:

{exp:channel:entries
    status="not Closed"
    channel="products"
    dynamic="no"
    url_title="{segment_2}"
    require_entry="yes"
    track_views="one"
    disable="member_data|pagination"}
    {if no_results}
            EMBED YOUR 404 TEMPLATE HERE
        {/if}
...

NOTE: An edit was made to the above answer stripping out quotes and curlies from the conditionals e.g. it was:

{if:elseif "{segment_3}" == "entries"}

So the amended answer is correct if you're using the latest version of EE (possible v2.9 onwards), but historically with older versions I've encountered numerous issues with conditional parsing if the segment or value is not defined ( {if:elseif segment_3 == "entries"} gets parsed as {if:elseif == "entries"} if segment_3 has no value and generates parsing errors. Hence the need to wrap in quotes and define as EE variables.

You can be even more smart with the URLs and do away with all the identifier segments ("entries", "list" and "details"), where you utilise the no_results tag pair for product details, no results conditional assume the listing, then on the listing detect if it's a top level category or sub category, or if it's a no result, then display 404. If you decide on this path, you'll probably need low2seg and GW Code Has Children.

| improve this answer | | | | |
  • Thanks Peter. I've tried doing it with low2seg and GW Code Has Children but just can't get it working. Also considered keeping same template structure and rewriting urls in htaccess file, but that won't work either. Currently if I try visiting an non-existant URL say products/ljkljlkjl/ it reverts to the products/index page, not the 404 like the rest of the site. Could this be having a effect? – James Curran Jul 2 '14 at 12:26
  • Ignoring the last paragraph in my answer, where have you got to with no extra add-ons and the code above? None of it's working? It should all be going through the products/index template - even the 404. That's why we need the no_results conditional to catch anything that isn't a valid URL. – Peter Lewis Jul 2 '14 at 16:12
  • Hi @peter sorry it's been so long. If you can still help I have tried with the code as follows: {if "{segment_2}" == ""} Display all top level product categories {embed=products/all} {if:elseif "{segment_2}" == "list"} Lists the products within current category {embed=products/list} {if:elseif "{segment_2}" == "list"} Lists the subcategories for any top level categories {embed=products/list} {if:elseif "{segment_2}" == "detail"} Shows the entry for the product {embed=products/detail} {if:else} 404! {/if} – James Curran Aug 8 '14 at 22:48
  • It embeds the "products/all" template but the shows the "products/list" template as a page itself and not embedded with "products/index" – James Curran Aug 9 '14 at 6:51
  • How can it embed the products/all template but show the products/list template - don't you mean it embeds both? If you have a template called "list" in the "products" template group, then it will use this template if your URL is /products/list. Are you using the word "category" in the URL or using the actual category name? – Peter Lewis Aug 12 '14 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.