Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2024-11-01 23:35:15

mwr
Member
From: Canada
Registered: 2006-01-31
Posts: 167
Website

Text output depending on number of search results

Is there there a way to have different text outputs depending on whether there is one result, multiple results or no results.

Examples:
1 item matches your search.
2 items match your search.
There is no match for your search.

Last edited by mwr (2024-11-01 23:46:17)


Mark

Offline

#2 2024-11-02 01:20:02

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,463
Website GitHub

Re: Text output depending on number of search results

Absolutely :)

<if::search_results max="1">
   1 item matches your search.
<txp:else />
    <if::search_results>
        <txp:search_results_count text="items" /> match your search.
    <txp:else />
        There is no match for your search.
    </if::search_results>
</if::search_results>

NB: under 4.9.0+ the <txp:search_results_count /> tag has been deprecated and is now called the more generic <txp:items_count /> because it works in places outside of just search.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2024-11-02 03:55:31

mwr
Member
From: Canada
Registered: 2006-01-31
Posts: 167
Website

Re: Text output depending on number of search results

Thanks, Bloke!

Going to assume you meant this:

<txp:if_search_results max=“1”> 1 item matches your search. <txp:else /> <txp:if_search_results> <txp:search_result_count text=“items” /> match your search. <txp:else /> There is no match for your search. </txp:if_search_results> </txp:if_search_results>

Last edited by mwr (2024-11-02 03:57:58)


Mark

Offline

#4 2024-11-02 08:41:40

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,463
Website GitHub

Re: Text output depending on number of search results

Same thing, yes. Up to you if you want to use the full txp: syntax or the shorthand. Depends on your settings, but it’s enabled by default.

I could have used <search::results_count /> too.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#5 2024-11-02 16:13:57

mwr
Member
From: Canada
Registered: 2006-01-31
Posts: 167
Website

Re: Text output depending on number of search results

Cool. I’ll have to learn more about shorthand.


Mark

Offline

Board footer

Powered by FluxBB