Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpressionEngine: Single entry template

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?

like image 728
geoffs3310 Avatar asked Oct 22 '12 14:10

geoffs3310


2 Answers

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.

like image 136
Christopher Kennedy Avatar answered Sep 25 '22 15:09

Christopher Kennedy


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"}
like image 45
Derek Hogue Avatar answered Sep 22 '22 15:09

Derek Hogue