5

I have created a channel called credit cards. So I have created a template group called credit-cards with an index which loops through all the credit cards and output them. This aspect works fine, here is my code for the index.html file inside credit-cards.group folder:

         {exp:channel:categories category_group="1" style="linear" dynamic="no"}
                    <div class="card-list tab" id="{category_url_title}">
                        <h2 class="category-title">{category_name} Credit Cards</h2>
                        <div class="cards">
                            {exp:channel:entries channel="credit_cards" category="{category_id}" dynamic="no"}
                                <article>
                                    <h4><a href="{url_title_path='credit-cards'}">{title}</a><span class="web-exclusive">MBNA Website Exclusive</span></h4>
                                    <ul>
                                        <li class="col-img">
                                            <a href="{url_title_path='credit-cards'}"><img width="116" height="84" alt="MBNA Platinum Credit Card" src="../lib-ui/img/cards/core/core_116x84/mbna_platinum_card_116x84.png"></a>
                                        </li>
                                        <li class="col-bt">{balance_transfer_rate}</li>
                                        <li class="col-purchases">{purchases_rate}</li>
                                        <li class="col-features">{key_features}</li>
                                        <li class="col-apply">
                                            <a rel="blank" class="btn btn-success" href="{apply_url}">
                                                Apply Now<span class="hide"> for the {title}</span>
                                            </a>
                                            <a class="cta" href="{url_title_path='credit-cards'}">
                                                Learn more<span class="hide"> about the {title}</span>
                                            </a>
                                            <label class="mbna-credit-card checkbox" for="compare_1">
                                                <span tabindex="0">
                                                    <input type="checkbox" value="mbna-credit-card" id="compare_1">
                                                </span>
                                                <span class="hide"> Add the {title} to </span>Compare
                                            </label>
                                        </li>
                                    </ul>
                                    <p class="rep-ex">{representative_example}</p>
                                </article>  
                            {/exp:channel:entries} 
                        </div>
                    </div>
                {/exp:channel:categories}

So my question is this. Say I have a credit card called visa credit card, the url that is being generated for it is /credit-cards/visa-credit-card. When I click this link though I just get my index page again. I have created another template file inside my group called single.html that has the code to output a single credit card. This looks like so:

<h1>Credit Card Page</h1>
{exp:channel:entries channel="credit_cards" limit="1"}
{if no_results}
{redirect="404"}
{/if}

So how do I get it to use this template file instead for a single entry?

geoffs3310
  • 5,599
  • 11
  • 51
  • 104

5 Answers5

10

This is actually a pretty easy to shore up issue. What you have going on is that line 5 of your code has no way to tell if it should limit the entry information. With dynamic='no', you've said "EE, you don't need to use the URL here to figure out what entries to limit this by"

My suggestion would be the following code:

{if segment_2 == ""}
    {exp:channel:categories category_group="1" style="linear" dynamic="no"}
        <div class="card-list tab" id="{category_url_title}">
            <h2 class="category-title">{category_name} Credit Cards</h2>
            <div class="cards">
                {exp:channel:entries channel="credit_cards" category="{category_id}" dynamic="no" disable="category_fields|member_data|pagination|trackbacks"}
                    <article>
                        <h4><a href="{url_title_path='credit-cards'}">{title}</a><span class="web-exclusive">MBNA Website Exclusive</span></h4>
                        <ul>
                            <li class="col-img">
                                <a href="{url_title_path='credit-cards'}"><img width="116" height="84" alt="MBNA Platinum Credit Card" src="../lib-ui/img/cards/core/core_116x84/mbna_platinum_card_116x84.png"></a>
                            </li>
                            <li class="col-bt">{balance_transfer_rate}</li>
                            <li class="col-purchases">{purchases_rate}</li>
                            <li class="col-features">{key_features}</li>
                            <li class="col-apply">
                                <a rel="blank" class="btn btn-success" href="{apply_url}">
                                    Apply Now<span class="hide"> for the {title}</span>
                                </a>
                                <a class="cta" href="{url_title_path='credit-cards'}">
                                    Learn more<span class="hide"> about the {title}</span>
                                </a>
                                <label class="mbna-credit-card checkbox" for="compare_1">
                                    <span tabindex="0">
                                        <input type="checkbox" value="mbna-credit-card" id="compare_1">
                                    </span>
                                    <span class="hide"> Add the {title} to </span>Compare
                                </label>
                            </li>
                        </ul>
                        <p class="rep-ex">{representative_example}</p>
                    </article>  
                {/exp:channel:entries} 
            </div>
        </div>
    {/exp:channel:categories}
{/if}
{if segment_2}
    {exp:channel:entries channel="credit_cards" limit="1" disable="category_fields|member_data|pagination|trackbacks"}
        <article>
            <h4><a href="{url_title_path='credit-cards'}">{title}</a><span class="web-exclusive">MBNA Website Exclusive</span></h4>
            <ul>
                <li class="col-img">
                    <a href="{url_title_path='credit-cards'}"><img width="116" height="84" alt="MBNA Platinum Credit Card" src="../lib-ui/img/cards/core/core_116x84/mbna_platinum_card_116x84.png"></a>
                </li>
                <li class="col-bt">{balance_transfer_rate}</li>
                <li class="col-purchases">{purchases_rate}</li>
                <li class="col-features">{key_features}</li>
                <li class="col-apply">
                    <a rel="blank" class="btn btn-success" href="{apply_url}">
                        Apply Now<span class="hide"> for the {title}</span>
                    </a>
                    <a class="cta" href="{url_title_path='credit-cards'}">
                        Learn more<span class="hide"> about the {title}</span>
                    </a>
                    <label class="mbna-credit-card checkbox" for="compare_1">
                        <span tabindex="0">
                            <input type="checkbox" value="mbna-credit-card" id="compare_1">
                        </span>
                        <span class="hide"> Add the {title} to </span>Compare
                    </label>
                </li>
            </ul>
            <p class="rep-ex">{representative_example}</p>
        </article>  
    {/exp:channel:entries} 
{/if}

Mind you, this isn't 100% accurate, as I stripped out your exp:channel:categories tag, but this should get you a result that limits based on a shortened URL like you've specified.

JSK NS
  • 3,346
  • 2
  • 25
  • 42
  • Hi Thanks for your help. I have no doubt this will work however it still means all the code is in the same file. It's not a very clean solution is there no way to seperate them so that it looks at index.html for the index page and single.html for a single entry? – geoffs3310 Oct 23 '12 at 07:19
  • 1
    Hey Geoffs, if you want to preserve the "credit-cards/visa-credit-card" url structure instead of "credit-cards/single/visa-credit-card" Chris's answer is the way to go. Chris is bascially checking if the url contains a value after "credit-cards/" in the url, then switches to showing a single entry. You can use a snippet or embed to split your code into a different file to keep it looking clean. – Patrick Pohler Oct 23 '12 at 21:20
  • 1
    If you want to make the template cleaner, you could use snippets to remove the main content blocks to separate files (although you'd need an add-on like Mountee or SnippetSync to save them as files). And rather than do if segment_2/segment_2=="", definitely check out Switchee. Doing separate (simple) conditions for if/if not isn't as bad as doing complex conditionals like if:else (because both conditions get parsed even though only one is displayed), but once you start using Switchee you'll find you have to use if statements a lot less. – Tyssen Oct 23 '12 at 23:18
6

So how do I get it to use this template file instead for a single entry?

Instead of:

{url_title_path='credit-cards'}

Use

{title_permalink="credit-cards/single"}
Derek Hogue
  • 4,589
  • 1
  • 15
  • 27
5

There are two primary ways you can use the template 'credit-cards/single' for the VISA credit card category entry.

First Option

'credit-cards/index' template would have:

{if segment_2 != ""}
    {embed="credit-cards/single" entry_id="{entry_id}"}
{/if}

'credit-cards/single' template would have:

<h1>Credit Card Page</h1>
{exp:channel:entries channel="credit_cards" limit="1" entry_id="{embed:entry_id}"}
{if no_results}
  {redirect="404"}
{/if}
... your code ...
{/exp:channel:entries}

 

Second Option

Rename 'credit-cards/single' to 'credit-cards/company' (or something more SEO relevant) and use it the default EE way.

'credit-cards/index' template would stay the same.

'credit-cards/company' template would have:

<h1>Credit Card Page</h1>
{exp:channel:entries channel="credit_cards" limit="1" entry_id="{entry_id}"}
{if no_results}
  {redirect="404"}
{/if}
... your code ...
{/exp:channel:entries}

In this second option the url would be site.com/credit-cards/company/visa-credit-card

I hope that helps. Let me know if I've misunderstood anything.

Stephen Callender
  • 538
  • 1
  • 5
  • 18
2

You'll probably want something like this in the index template:

{if segment_2 != ""}
  {exp:channel:entries channel="credit_cards"}
    [single entry code...]
  {/exp:channel:entries}
{if:else}
  {exp:channel:categories category_group="1" style="linear" dynamic="no"}
    [your code for all credit cards]
  {/exp:channel:entries}      
{/if}

What's happening is the URL /credit-cards/visa-credit-card is loading the index template of the credit-cards template group, but the visa-credit-card URL title at the end tells ExpressionEngine to treat the template as a single entry page.

But the the dynamic="no" in your exp:channel:entries tag is why EE is still showing all the credit cards on that page. This free video about dynamic="off" might explain it better.

unexplainedBacn
  • 768
  • 4
  • 13
0

Thanks for all the great suggestions. I used the pages modules in the end which allows you to specify which template to use.

geoffs3310
  • 5,599
  • 11
  • 51
  • 104