Texpattern context demo
Section | Article |
---|---|
context_demo | 136 |
Date context
Txp’s date context is poorly documented, and not especially easy to use. So it’s not surprising that it remains little known. But it is a useful type of metadata context, and is the key to creating date-based article archives, a popular feature request.
article
is the only tag sensitive to date context. And there is no core tag for creating date context, or even links to such a context. Date context is a global context only, meaning it must be set in the URL, through the month
query string parameter. (Why “month” instead of “date”? I have no idea.)
You can create a date-based archive menu using only core tags. This may seem like a lot of work when there are plugins available, but you get complete control over how to format the menu. Note that the month
parameter must be part or all of a MySQL date string, i.e., YYYY-MM-DD format. Partial month
values can be missing any number of characters at the end, but must start from the beginning. E.g., month=200
indicates the date range 2000-01-01 to 2009-12-31.
You make the menu with an article_custom
tag (or article
if you want a context-sensitive menu) using if_different
to compare posted
values. A code example is the best explanation — here is a simple menu with links to year-based date contexts (try one of the links to see more examples):
This code:
<div class="archive-menu">
<txp:article_custom limit="9999" sort="Posted" break="">
<txp:if_different>
<a href="<txp:site_url />context_demo/?month=<txp:posted format="%Y" />">
<txp:posted format="%Y" />
</a>
</txp:if_different>
</txp:article_custom>
</div>
produces this output: