1

I am using pages to display content on a page called Speed Sales. I display the entry by checking for segment_2 and matching it to a channel entry basically. I need to display content in case someone spells the url wrong and it doesn't match a url_title in my speed_sales channel. Any ideas?

I'm assuming I might have to include a channel:entries tag with the channel="speed_sales" and check if segment_2 == a url_title for an entry, then display default content if it doesn't, but am unsure how to do this correctly.

EDIT:

I have updated my template to the following:

<article class="text-block">
        {exp:channel:entries channel="speed_sales" dynamic="no" url_title="{segment_2}"}
                {if url_title == "{segment_2}"}
                    <h2 class="heading1 excludestrip single left">{title}</h2>
                    {if lead_copy}<p class="lead">{lead_copy}</p>{/if}
                    {if subtitle}
                        <hr/>
                        <h3 class="excludestrip">{subtitle}</h3>
                    {/if}
                    {newsletter_copy}
                {/if}
                {if segment_2 == "" || no_results}
                    <h2 class="heading1 single">Speed Sales</h2>
                    <p class="lead">You may have reached this page in error (a typo in a link, or you backspaced the URL). Please choose a newsletter from below:</p>
                        {!-- {embed="speed_sales/.newsletter_list"} --}
                {/if}
        {/exp:channel:entries}
</article>

The reason I included the segment_2 check in my no_results conditional, is because it displays nothing in the no_results conditional (since there is no segment_2). The page url this would need to show up on is /speed_sales or anything under /speed_sales. What happens is everything works properly, but the if no_results conditional block displays multiple times instead of just once. It must be reading the amount of entries in the channel and repeating it that many times. I commented out the embedded list template to check, and it still repeats the block anyways.

| improve this question | | | | |
  • I've noticed that the no_results conditional doesn't repeat, but if I want to place this block on the speed_sales page itself by using the {if segment_2 == ""} then it repeats that block. Any ideas on how to get around that? – Alexa L Jul 16 '13 at 20:27
2

The no_results conditional should work, something like:

{exp:channel:entries channel="speed_sales" dynamic="no" url_title="{embed:url_title}"}
    {if no_results}
        <!-- Content for no results -->
        {if:else}
        <!-- Otherwise ... -->
            <h2>{title}</h2>
            {if lead_copy}<p class="lead">{lead_copy}</p>{/if}
            {if subtitle}
                <hr/>
                <h3>{subtitle}</h3>
            {/if}
        {newsletter_copy}
    {/if}
{/exp:channel:entries}

(code untested)

See http://ellislab.com/expressionengine/user-guide/modules/channel/channel_entries.html#if-no-results for more info.

| improve this answer | | | | |
  • 2
    This is what I would suggest but instead of using if:else just use a simple conditional {if no_results} some content {/if}. – CreateSean Jul 11 '13 at 20:29
  • Thanks! I forgot the no_results conditional. I will give this a shot and let you guys know. – Alexa L Jul 15 '13 at 20:54
  • I tried implementing something similar to this, and took this embed and put it in my main template, but the no_results block is being repeated multiple times inside the channel entries tag, instead of showing once. – Alexa L Jul 16 '13 at 17:18
0

If there's no segment_2, the channel:entries variable pair will parse as:

{exp:channel:entries channel="speed_sales" dynamic="no" url_title=""}

See? The docs says that the default value of the limit parameter is 100. So, this loop will runs up to one hundred times!

My suggestion is, first, to put your code for no_results on a snippet and set its as, let's say, snip_no_speed_sales:

<h2 class="heading1 single">Speed Sales</h2>
<p class="lead">You may have reached this page in error (a typo in a link, or you backspaced the URL). Please choose a newsletter from below:</p>

Then, you need to separate your conditionals to avoid the parsing of channel:entries variable pair and to improve the performance.

<article class="text-block">
    {if segment_2}
    {exp:channel:entries channel="speed_sales" dynamic="no" url_title="{segment_2}"}
        <h2 class="heading1 excludestrip single left">{title}</h2>
        {if lead_copy}<p class="lead">{lead_copy}</p>{/if}
        {if subtitle}
            <hr/>
            <h3 class="excludestrip">{subtitle}</h3>
        {/if}
        {newsletter_copy}
        {if no_results}
            {snip_no_speed_sales}         
        {/if}
    {/exp:channel:entries}
    {/if}
    {if segment_2 == ''}
        {snip_no_speed_sales}
    {/if}
</article>
| improve this answer | | | | |
  • Ahh that looks like it might fix my dilemma, I'll try it out tomorrow thanks! – Alexa L Jul 18 '13 at 6:01

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.