Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Display section only if live article exists in section
OK, I’m having a memory blank here.
How can I display a chunk of code on a site homepage only if a live article exists in a section, so for example, only display this code below if there actually is a live article in current-news
section…
<section class="current-news">
<h2>Latest news</h2>
<article>
<h3>The current news article txp:title</h3>
<p>The txp:excerpt text of current news article</p>
</article>
</section>
I’m sure I’m forgetting something obvious to achieve this.
Offline
Re: Display section only if live article exists in section
The quick and dirty method:
<txp:variable name="article-check"><txp:article_custom section="current-news" limit="1" /></txp:variable>
<txp:if_variable name="article-check">
<section class="current news">
<h2>Latest news</h2>
<article>
<txp:article_custom section="current-news" limit="1">
<h3><txp:title /></h3>
<p><txp:excerpt /></p>
</txp:article>
</article>
</section>
</txp:if_variable>
You can probably optimise it to only include one call to <txp:article_custom>
to save a DB call.
Last edited by springworks (2014-03-27 13:10:38)
Offline
Re: Display section only if live article exists in section
I’ve just found that simply using…
<txp:article_custom section="current-news" limit="1">
<section class="current-news">
<h2>Latest news</h2>
<article>
<h3><txp:title /></h3>
<txp:excerpt />
</article>
</section>
</txp:article_custom>
…gives the desired functionality. It won’t render anything unless there is an article available, works as intended to not display expired or hidden articles too. Result!
Offline
Re: Display section only if live article exists in section
Of course, well done!
Offline