Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2007-12-27 17:10:53

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Message if no articles in category

Can’t quite find what I am looking for – I need to display a message to say something like “Sorry, no articles in this category” if there are no articles within that category.

Code:

<txp:if_category>
<h3>All articles in the <txp:category title="1" link="1" /> category.</h3>
<txp:article form="classifieds_list" limit="100" />
<txp:else />
<txp:article form="classifieds_list" limit="5" />
</txp:if_category>

The code above outputs the message “All articles in the —— category.” That’s fine, but no good if there are no articles. Anyone know of a plugin for this?

Offline

#2 2007-12-27 17:16:49

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

Re: Message if no articles in category

Can chh_if_data help?


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 2007-12-27 17:22:03

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: Message if no articles in category

Yeah I saw that, and pm_if_article, but not got them working the way I want yet.

Offline

#4 2007-12-27 17:29:22

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,091
Website GitHub Mastodon Twitter

Re: Message if no articles in category

what about swf_if_empty ?


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#5 2007-12-27 17:36:06

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

Re: Message if no articles in category

Aha, yes, it’ll probably require a slight change in your logic because, regardless if there any articles or not, the txp:category tag will return ‘true’ thus always executing the first part.

You could get round it like this:

<txp:if_category>
<h3>Articles in the <txp:category title="1" link="1" /> category</h3>
<txp:chh_if_data>
<txp:article form="classifieds_list" limit="100" />
<txp:else />
<p>Sorry, no articles found.</p>
</txp:chh_if_data>
<txp:else />
<txp:article form="classifieds_list" limit="5" />
</txp:if_category>

Any good? Or -swf_if_empty dang, colak beat me to it.

Last edited by Bloke (2007-12-27 17:38:52)


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

#6 2007-12-27 17:53:55

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: Message if no articles in category

Thanks guys. Almost there. Funnily enough, I had just downloaded chh_if_data to test it out. I have not heard of swf_if_empty – if this does not work, I will try that.

Currently:

<txp:if_category>
<txp:chh_if_data>
<h3>All articles in the <txp:category title="1" link="1" /> category</h3> 
<txp:article form="classifieds_list" limit="100" />
<txp:else />
<h3>Sorry, no articles in the <txp:category title="1" /> category.</h3>
</txp:chh_if_data>
<txp:else />
<txp:article form="classifieds_list" limit="5" />
</txp:if_category>

Hmm. In the category page where there are no articles, I still get: “All articles in the ….. category”. Pain in the ###!

Offline

#7 2007-12-27 18:25:14

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

Re: Message if no articles in category

jstubbs wrote:

Hmm. In the category page where there are no articles, I still get: “All articles in the ….. category”.

You will do. You have 2 TXP tags surrounded by the chh_if_data and else tags (namely txp:category and txp:article). It tests them both. txp:article will return nothing as it should but txp:category returns the category name, thus chh_if_data thinks “aha, some output” and will display the first part, NOT the else as you expect.

Took me a while to figure that out when I first used it; confused the heck out of me. See my post above for a (possible) way round it.

Last edited by Bloke (2007-12-27 18:27:13)


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

#8 2007-12-27 18:44:48

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: Message if no articles in category

Thanks Stef. I will go with your code for now – I do get a proper error message like this:

“All articles in the …. category:
Sorry, no classifieds found in this category.”

So, no way around this that you know of? Would be nice to get rid of the “All articles in the … category:” part, since there aren’t any. I am a perfectionist ;-)

Offline

#9 2007-12-27 18:47:22

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: Message if no articles in category

I should also own up: That was not the complete code, I had a bit more:

<txp:if_article_list>
<txp:if_section name="articles">
<txp:if_category>
<h3>All articles in the <txp:category title="1" link="1" /> category:</h3>
<txp:chh_if_data>
<txp:article form="classifieds_list" limit="100" />
<txp:else />
<p>Sorry, no articles found in this category.</p>
</txp:chh_if_data>
<txp:else />
<txp:article form="classifieds_list" limit="5" />
<txp:ob1_pagination firsttext="First page" previoustext="Previous page" nexttext="Next page" lasttext="Last page" ulclass="pagination" />
</txp:if_category>
</txp:if_section>
</txp:if_article_list>

Edit: Placed the code here for completeness.

Last edited by jstubbs (2007-12-27 18:48:49)

Offline

#10 2007-12-27 19:03:17

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

Re: Message if no articles in category

jstubbs wrote:

So, no way around this that you know of? I am a perfectionist ;-)

Hehehe, not unless you reword the h3 or put the <txp:category /> outside the chh_if_data somehow, sorry.

However, I’m not sure what would happen if you used:

<txp:chh_if_data>
<h3>All articles in the <txp:php>echo category(array("title" => "1", "link" => "1"));</txp:php> category</h3> 
<txp:article form="classifieds_list" limit="100" />
<txp:else />
<h3>Sorry, no articles in the <txp:category title="1" /> category.</h3>
</txp:chh_if_data>

chh_if_data may just look at the “return value” of the txp:php call, which could well be nada in this case. EDIT: it doesn’t, it reads the result of the contents of the PHP call, so this method won’t work. Humbug.

Last edited by Bloke (2007-12-27 20:59:23)


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

#11 2007-12-27 19:49:37

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: Message if no articles in category

he-he, it returns nothing! I will just go for now with your first code offering…thanks!

Offline

Board footer

Powered by FluxBB