Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2014-03-27 12:45:47

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

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

#2 2014-03-27 12:55:18

springworks
Member
Registered: 2005-01-06
Posts: 172
Website

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

#3 2014-03-27 13:15:22

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

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

#4 2014-03-27 17:49:44

springworks
Member
Registered: 2005-01-06
Posts: 172
Website

Re: Display section only if live article exists in section

Of course, well done!

Offline

Board footer

Powered by FluxBB