Textpattern tips, tutorials and code snippets

Extend if_different using the variable and if_variable tags

The txp:if_different> tag can be used to display the contained statement only when its value differs from the preceding value. An example is – when you publish more than one article per day – to display the posting time not for every article but once per day:

<txp:if_different>
   <!-- heading - only once per day -->
   <h3><txp:posted format="%Y %e %B" /></h3>
</txp:if_different>
<h4><txp:permlink><txp:title /></txp:permlink></h4>
<txp:body />

The tag has the limitation that it can only contain one other txp tag, so if you would like to display other parts of the article beside the date, if_different won’t work.

You can solve that by using the variable and if_variable tags.

<txp:variable name="date" value='<txp:if_different><txp:posted format="%Y %e %B" /></txp:if_different>' />

<txp:if_variable name="date" value="">
   <!-- the date does not differ from the previous one, don't display anything -->
<txp:else />

   <!-- display the date once -->
   <h3><txp:posted format="%Y %e %B" /></h3>

   <!-- display the day's most recent article in full -->
   <h3><txp:permlink><txp:title /></txp:permlink></h3>
   <txp:body />

</txp:if_variable>

<!-- display the permlinked article titles for the other articles published on this date -->
<txp:if_variable name="date" value="">
<p><txp:permlink><txp:title /></txp:permlink></p>
</txp:if_variable>

2 Comments Comment feed

  • Jens Freyer
  • 15 June 2016

I'm searching for a possibility to create an archive overview which will link by <txp:posted format="%B %Y"/> to ALL articles posted for a month (please compare with the draft of my blog frickeln.jensfreyer.de/blog/. Is there a chance to do this with <txp:if_different>?

Hi Jens I think Michael has a method that can help you over on his forum thread

Add a comment

Use Textile help to style your comments