Custom content if data exists

One of Textpattern’s hidden gems is a plugin called chh_if_data. This plugin outputs conditional content if data exists within a TXP tag.

This TXP Tip provides a simple example of how to display content based on the results of a conditional statement.

  1. Download and install chh_if_data

Enter some code in your page template

<txp:if_section name="news">
<txp:if_individual_article>
<txp:article form="news_single" limit="1" />
<txp:else />
<div id="newsbox">
<h2>Site Announcements</h2>
<txp:chh_if_data>
<txp:article_custom section="news" form="news_announcement" category="Announcements" limit="1" />
<txp:else />
<p>There are no current announcements.</p>
</txp:chh_if_data>
</div>
<h2>News</h2>
<txp:article form="news_list" limit="10" />
<txp:ob1_pagination firsttext="First page" previoustext="Previous page" nexttext="Next page" lasttext="Last page" ulclass="pagination" />
</txp:if_individual_article>
</txp:if_section>

Most of the example code is self-explanatory. This is the important part:

<txp:chh_if_data>
<txp:article_custom section="news" form="news_announcement" category="Announcements" limit="1" />
<txp:else />
<p>There are no current announcements.</p>
</txp:chh_if_data>

The chh_if_data plugin here checks if there is any data if we are in an article list in the news section. If there is data, show one article from the Announcements article category.

If there is no data, the plugin displays a short message “There are no current announcements”.

For an example, visit the Snoooz Tennis news page.

comments powered by Disqus