Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-05-27 15:15:28

beechy
Member
Registered: 2006-03-02
Posts: 92

Only display categories that contain live articles

I am using <txp:category_list > to display the categories on my website in the blog. However I also use categories for some of my sticky articles both category1 and category2.

On the blog I only want to display categories that contain live articles – is this possible? at the moment all of the categories on my site display. I know you can do a exclude=”“ but I would have to do this manually everytime I create a new category is there a better way.

Thanks

Offline

#2 2010-05-27 15:49:26

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: Only display categories that contain live articles

Here is a TXP Tip that shows how to do it for sections (using the variable and if_variable tags). You can do the same for categories.

Last edited by els (2010-05-27 15:49:54)

Offline

#3 2010-05-28 10:17:36

beechy
Member
Registered: 2006-03-02
Posts: 92

Re: Only display categories that contain live articles

Hi Els thanks for the response. I think changing your example to suit my needs is beyond my textpattern skills currently, any chance of a few pointers in terms of the actual code.

Thanks

Offline

#4 2010-05-28 10:46:28

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: Only display categories that contain live articles

Of course :) Something like this:

<txp:category_list wraptag="ul" break="li">
	<txp:variable name="haslivearticles" value='<txp:article_custom category=''<txp:category />'' limit="1" />' />
	<txp:if_variable name="haslivearticles" value="">
		<!-- no live articles, don't display the category -->
	<txp:else />
		<txp:category title="1" link="1" />
	</txp:if_variable>
</txp:category_list>

The article_custom tag displays only live articles as long as you don’t explicitly set status="sticky". So if there are only sticky articles in a particular category, the value of the variable will be empty. Only if there is at least one live article, the category will be displayed in the list.

Offline

#5 2010-05-28 11:04:35

beechy
Member
Registered: 2006-03-02
Posts: 92

Re: Only display categories that contain live articles

Els that is awsome many thanks for that;

I have to make one edit to the code: I changed break=“li” to break =”“ and added <li><txp:category title=“1” link=“1” /></li> as it was adding <li></li> for no live articles: so my code now looks like this:

<txp:category_list wraptag="ul" break="">
	<txp:variable name="haslivearticles" value='<txp:article_custom category=''<txp:category />'' limit="1" />' />
	<txp:if_variable name="haslivearticles" value="">
		<!-- no live articles, don't display the category -->
	<txp:else />
		<li><txp:category title="1" link="1" /></li>
	</txp:if_variable>
</txp:category_list>

Last edited by beechy (2010-05-28 11:11:13)

Offline

Board footer

Powered by FluxBB