SPIP

[ar] [bg] [ca] [cpf] [cs] [da] [de] [en] [eo] [es] [eu] [fa] [fon] [fr] [gl] [id] [it] [ja] [lb] [nl] [oc] [pl] [pt] [ro] [sv] [tr] [vi] [zh] Espace de traduction

Download the latest version of SPIP

SPIP 1.9.2 SPIP 1.9.2
Home page > English documentation > Webmasters > A few examples of loops > Automatically displaying by date or other imposed order

Automatically displaying by date or other imposed order

November 2003

All the versions of this article:


The situation is as follows: within the same site, the presentation of articles in various sections must be differentiated:

-  in some sections, the articles are published one after the other, we therefore want them displayed in chronological order with the most recent at the top of the list and the oldest at the bottom.

-  in other sections, we want to control the order of articles displayed on the public site by numbering them ("1. The first article", "2. The second article", etc.).

These effects can be achieved as follows:

We are in a section loop, (for example, the page "rubrique.html"):

Inside this loop, we carry out the following test: is there an article in this section which has a title starting with a number, or not?

The interesting criterion is this:

This selects an article by title according to a regular expression which evaluates titles with one or more numbers (in the range 0 to 9) immediately following by a dot, at the beginning of the title. The syntax is as follows: == indicates selection by regular expression; ^ indicates the beginning of the string to be tested; "[0-9]" means "the range of characters between 0 and 9 inclusive"; + indicates at least one of the preceding characters; and \. indicates the dot character.

Note, this only selects one such numbered article ({0,1}); this way, the inside of the loop is only executed once. Moreover, even if there is only one numbered article it will still cause the list to be displayed in "numbered" order.

The above loop displays the message "A numbered article exists in this section." if the section contains at least one article with a title which begins with a number, otherwise it displays "There are no numbered articles."

These messages can now be replaced with nested loops which will display the articles in the desired presentation order:


ٍShow the template of this page Site created with SPIP | Translation area | Private area