Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-02-01 00:53:02

jonhicks
Member
From: Oxfordshire UK
Registered: 2004-03-22
Posts: 256
Website

Textpattern articles grouped by year?

I’m trying to get Textpattern to output a list of articles, grouped in divs by year. The output should look like:

<div id=“news2007”>
…list of articles from 2007…
</div>

<div id=“news2006”>
…list of articles from 2006…
</div>

To do this, I’m calling a form using:

<txp:article limit=“1000” form=“list_news_byyear” />

and the ‘list_news_byyear’ form uses txp:if_different like this:

<txp:if_different>
<div id=“news<txp:posted format=”%Y” />”>
</txp:if_different>
<p><txp:posted format=”%d.%m.%y” /><br />
<a href=”<txp:permlink />”><txp:title /></a></p>
<txp:if_last_article></div></txp:if_last_article>

Which almost works, except that only the last div gets closed by the <txp:if_last_article></ul></div></txp:if_last_article>

One way around it is to use the month attribute when calling the form:

<txp:article_custom section=“news” limit=“100” form=“list_news_byyear” month=“2007” />
<txp:article_custom section=“news” limit=“100” form=“list_news_byyear” month=“2006” />

But that requires manually adding years – a more automated solution would be good! Can anyone see a way around this?

Last edited by jonhicks (2008-02-01 01:01:26)


Cheers,
Jon VC#9

Offline

#2 2008-02-01 01:10:17

redbot
Plugin Author
Registered: 2006-02-14
Posts: 1,410

Re: Textpattern articles grouped by year?

Maybe you could do so:

<txp:if_different>
<txp:if_first_article><else /></div></txp:if_first_article>
<div id="news<txp:posted format="%Y" />">
</txp:if_different>
<p><txp:posted format="%d.%m.%y" /><br />
<a href="<txp:permlink />"><txp:title /></a></p>
<txp:if_last_article></div></txp:if_last_article>

Basically if you are not listing the first article it should add a closing div before the opening div.
Note that I’ve not tested it and I don’t know if makes sense at all

[Ruud: fixed quotes]

Offline

#3 2008-02-01 01:24:40

jonhicks
Member
From: Oxfordshire UK
Registered: 2004-03-22
Posts: 256
Website

Re: Textpattern articles grouped by year?

Thanks for the suggestion – its a great idea and that should work (I tried some other combinations), but it results in no output at all. It feels close though!


Cheers,
Jon VC#9

Offline

#4 2008-02-01 10:45:59

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Textpattern articles grouped by year?

What you need is a tag construct that doesn’t exist:

<txp:if_different>
  <txp:posted format="%Y" />
<txp:then />
  <txp:if_first_article><else /></div></txp:if_first_article>
  <div id="news<txp:posted format="%Y" />">
</txp:if_different>

You can emulate that with a bit of PHP (not tested):

<txp:php>
  global $oldyear = 0;
  $year = posted(array('format' => '%Y'));
  if ($year !== $oldyear)
  {
     echo if_first_article(array(), '<else /></div>');
     echo '<div id="news'.posted(array('format' => '%Y')).'">'
  }
  $oldyear = $year;
</txp:php>

Last edited by ruud (2008-02-01 10:47:26)

Offline

#5 2008-02-01 12:20:54

redbot
Plugin Author
Registered: 2006-02-14
Posts: 1,410

Re: Textpattern articles grouped by year?

ruud wrote:

What you need is a tag construct that doesn’t exist:

Ah yes you’re right, thanks

Offline

Board footer

Powered by FluxBB