Hide articles where the time is set to 00:00
Sometimes you’ll want to use Textpattern articles as events. A common approach is to use Textpattern’s “Posted Date/Time” field to store date/time information about the event. For example, user “totoff” at the TXP forums was using the
<txp:posted /> tag to output the time of a vernissage (a private viewing that occurs before a gallery opening). He wanted to show only articles where the time wasn’t set to “00:00”, because this is the default setting and would indicate that there was no vernissage.
Our goal is to compare the posted date with the value “00:00” and see if there’s a match. If there is a match, there is no vernissage, so we’ll do nothing. If there is a mismatch, it means there is a vernissage, and we’ll output the vernissage data.
Store the information in a variable
First, let’s store the posted information in a variable, so that we can compare it later. One trick I learned about variables from Stef Dawson is that you can use them as a container tag, like so:
<txp:variable name="vernissage_time"><txp:posted format="%H:%M" /></txp:variable>
That creates a new variable called “vernissage_time” and sets its value to the posted date/time as “HH:MM” (Hours/Minutes).
Check the content of the variable
Next, we want to use
<txp:if_variable> to check if
vernissage_time is set to 00:00.
<txp:if_variable name="vernissage_time" value="00:00">
If that’s true, let’s do nothing. I like to put a comment in, like this:
<!-- no vernissage -->
Then let’s use the
<txp:else /> statement for when we’ve got a time that’s different than “00:00” and thus a vernissage is being held:
<txp:else /> <h2>Vernissage Time:</h2> <p><txp:variable name="vernissage_time" /></p> </txp:if_variable>
That’s done it! Here is the complete code:
<txp:variable name="vernissage_time"><txp:posted format="%H:%M" /></txp:variable> <txp:if_variable name="vernissage_time" value="00:00"> <!-- no vernissage --> <txp:else /> <h2>Vernissage Time:</h2> <p><txp:variable name="vernissage_time" /></p> </txp:if_variable>