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 1


A book

Group 16



Group 27


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>

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>

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