ExpressionEngine no_results not working
Asked Answered
G

3

5

Hi I have the following code:

<div class="row-fluid">
    <div class="span12">
        <h3>Top 10 most popular</h3>
        <ul>
            {exp:channel:entries channel="faqs" dynamic="no" limit="10"}
                <li><a href="{url_title_path='help'}">{title}</a></li>
            {/exp:channel:entries} 
        </ul>

        <h3>Answer to selected question</h3>
        {exp:channel:entries channel="faqs" dynamic="yes" require_entry="yes" limit="1"}
             {if no_results} 
                <p>Click a question above to see the answer here</p>
             {/if}
            {answer}
        {/exp:channel:entries} 
    </div>    
</div>

So as you can see I am looping through faqs twice. The first set it to get all of the questions and then if a user clicks on a question thats when the second loop should kick in to show them the answer. This works fine apart from the no results statement in the second loop doesn't work. It just never gets output. Anyone know why?

Greenman answered 29/10, 2012 at 10:29 Comment(5)
Are both loops displayed on the same page, with no other entry_id or url_title segments?Linguistic
Are you by chance using Stash and the "template partials" approach in your templates? no_results does not work as expected in that circumstance. If that is the case for you then this thread may help.Seko
Yeah I am using stash. Sorry I didn't realise this was the problem otherwise I would have mentioned itGreenman
I don't know for sure that it is now that I look at it. I've got a few sites using template partials approach and the no_results tag seems to be working just fine to redirect to the 404 template, though I could have sworn I'd seen it not working in the past. What version of Stash are you using?Seko
The thread I linked to in my comment above is pretty outdated. I found that getting no_results working with Stash was just a matter of using the beta version and the no_results_prefix (see my answer below).Seko
M
8

You'll have to add

require_entry="yes"

to your second loop in order for the no results to work.

See the docs here http://expressionengine.com/user_guide/modules/channel/channel_entries.html#require-entry

Makassar answered 29/10, 2012 at 10:34 Comment(1)
That's still not working. I've updated the code in the question to reflectGreenman
S
2

Since you mentioned in the comments that you are using Stash, this following should get no_results working as expected. I've just tested this and it works for me.

Download the beta version of Stash here https://github.com/croxton/Stash/tree/dev (currently v2.3.5).

See the documentation on no_results_prefix on the GitHub page.

You did not show the surrounding Stash tag that wraps your template code, but assuming it is a stash:set tag, update your template something like this:

{exp:stash:set name="page_content" no_results_prefix="stash"}
    <div class="row-fluid">
        <div class="span12">

            [...]

            <h3>Answer to selected question</h3>
            {exp:channel:entries channel="faqs" dynamic="yes" require_entry="yes" limit="1"}
                {!-- note prefix, which was defined in outer stash:set tag --}
                {if stash:no_results} 
                    <p>Click a question above to see the answer here</p>
                {/if}
                {answer}
            {/exp:channel:entries} 
        </div>    
    </div>
{/exp:stash:set}
Seko answered 16/11, 2012 at 22:1 Comment(0)
G
0

I reported this to EllisLab as a bug. It is definitely not working as it should. https://support.ellislab.com/bugs/detail/21106

Gestalt answered 16/9, 2015 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.