Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Help with dynamic year/month archive menu
Hello
I’m migrating a site from WordPress to TextPattern – with 6 years of posts in a section called ‘news’.
I’d like to make a simple-looking 2-line horizontal archive menu which lists years in the first line and, once you’ve clicked a year, lists the months on the second line, and clicking a month shows the articles for that year/month. Something like this:
2011 2010 2009 2008 2007 2006
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
I can almost achieve this with upm_date_archive – but it lists each year on a separate line. Could anyone please suggest how I might otherwise do this?
Thanks for your help.
Offline
Re: Help with dynamic year/month archive menu
Hacking upm_date_archive would be expedient enough. upm_date_archive appears not to have been updated in a while, so it’s not likely you’d have the problem of having to maintain your hack through successive official upgrades. You can do this by clicking the “Edit” link for upm_date_archive in the Plugins list.
A plugin-free version of your menu is possible. It might look something like this bunch of untested code:
Edit: modified as per my note a couple of posts later
<txp:if_section name="news">
<txp:article_custom section="news" limit="9999" break="">
<txp:if_different>
<a href="<txp:section url="1" />?month=<txp:posted format="%Y" />">
<txp:posted format="%Y" />
</a><br />
</txp:if_different>
</txp:article_custom>
</txp:if_section>
That’s for the year menu. The month menu would involve similar code.
Last edited by jsoo (2011-01-11 14:32:49)
Code is topiary
Offline
Re: Help with dynamic year/month archive menu
Excellent .. thanks for your help jsoo. I’ll post the final code here when I’ve finalised it.
Offline
Re: Help with dynamic year/month archive menu
Just tried a quick test and realized a couple of things:
You want break=""
in the article tag, otherwise you get the break for every article.
I don’t know why, but this seems to work with article_custom
, but not with article
.
Code is topiary
Offline
Re: Help with dynamic year/month archive menu
Okay, this is working for single years:
<txp:article section="news" limit="9999" wraptag="ul" class="year" break="li"> <txp:if_different> <a href="../?month=<txp:posted format="%Y" />"><txp:posted format="%Y" /></a> </txp:if_different> </txp:article>
<txp:article section="news" limit="9999" wraptag="ul" class="month" break="li" sort="Posted asc"> <txp:if_different> <a href="../?month=<txp:posted format="%Y-%m" />"><txp:posted format="%b" /></a> </txp:if_different> </txp:article>
.. but, if there are multiple years (as there are), the months are duplicated, eg:
2010 2009
Oct Oct Nov Dec
I’m guessing I’ll need to use some sort of conditional for each year (perhaps smd_if) .. I’ll have a go, meanwhile any suggestions welcome.
Thanks again.
Offline
Re: Help with dynamic year/month archive menu
I think you’ll have to use a bit of raw PHP to grab the year from the URL: Edit: Stupid idea; use page_url
instead. Code updated.
<txp:variable name="year" value='<txp:page_url type="month" />' />
<txp:if_variable name="year" value=""><txp:else />
<txp:article_custom month='<txp:variable name="year" />' section="news" limit="9999" wraptag="ul" class="month" break="" sort="Posted asc">
<txp:if_different>
<li><a href="../?month=<txp:posted format="%Y-%m" />"><txp:posted format="%b" /></a></li>
</txp:if_different>
</txp:article_custom>
</txp:if_variable>
As noted above, you’ll want to use break=""
in the article tags, then add the li
tags inside the if_different
blocks, as here.
Last edited by jsoo (2011-01-17 19:05:25)
Code is topiary
Offline
Re: Help with dynamic year/month archive menu
Brilliant, thanks again jsoo, that’s really helpful.
Offline
Re: Help with dynamic year/month archive menu
The raw PHP step is not needed: “ page_url
does the job. I updated the example code accordingly.
I only just learned this about page_url
, a tag that’s been around since Txp 4.0.2. There’s always something to learn …
Code is topiary
Offline
Re: Help with dynamic year/month archive menu
Even better .. thanks again Jeff.
Offline