Grouping articles

30 August 2012

Keywords: group, divide

The original question is here : group the articles in blocks of two. Here is an etc_query solution.

Group 15

Timeline

etc_url

Group 26

etc_query

We use article_custom here, but it can be replaced by <txp:article /> in page form:

<txp:etc_query data='<txp:article_custom form="titles" />'
 query="*[position() mod 2 = 1]">
<div class="group">
  <h2>Group {#row}</h2>
  {.|following-sibling::*[1]}
</div>
</txp:etc_query>

The titles article form we use is:

<p class="entry-title"><txp:permlink><txp:title /></txp:permlink></p>

You can change it as you like, just wrap article items in some top-level tag (p here).

If you need to group articles by 3, just use *[position() mod 3 = 1] and following-sibling::*[position()<3] selectors in etc_query. You can even make this grouping flexible, depending on some parameter, say, a GET variable group:

<txp:etc_query data='<txp:article_custom form="titles" />'
  query="*[position() mod {?group|2|intval} = 1]" globals="_GET">
  <h2>Group {#row}</h2>
  {.|following-sibling::*[position()<{?group|2|intval}]}
</txp:etc_query>

You can test it here by appending &group=3 to the url string.

File(s)