customizing txp:recent_articles

customizing txp:recent_articles, the first in a series of layout recipes for Textpattern.

There have been a number of recent queries on the textpattern support forums about ways of customizing the output of <txp:recent_articles />, to show excerpts, dates and so on.

If you need a list of recent articles with additional information or formatting, <txp:recent_articles /> is almost certainly not the right tool for the job. You’ll be far better served by the oft-misunderstood <txp:article_custom /> tag.

Like many new Texpattern users, I initially assumed <txp:article_custom /> was just <txp:article /> with more options. It isn’t. <txp:article /> is context-sensitive; depending on the page, it will display a single article, a list of all articles, a list of articles in a particular section or category, and the search results. <txp:article_custom /> does none of these things. It always displays a list, and ignores the current section and article context.

To display a simple list of article titles, first create a new form called headline. Set the Form Type to article, and enter the following form code:


<li><txp:permlink><txp:title /></txp:permlink></li>

Now save that and edit your page code. Use the following in place of txp:recent_articles, where you want the list to appear:


<ul>
<txp:article_custom form="headline" />
</ul>

Your page should now display the 10 most recent articles as an unordered list. If you’re seeing a blank page instead, check the the syntax of your form code and article_custom tag carefully. If the page displays normally, but the list is empty, make sure the form name in your article_custom tag exactly matches the name of the form you created.

You can change the number and type of articles displayed in the list by changing the article_custom attributes. Some examples:

A shorter list:
<txp:article_custom form="headline" limit=5 />

Only show articles from the “news” section:
<txp:article_custom form="headline" section="news" />

Only show articles in the “local” category:
<txp:article_custom form="headline" category="local" />

Only articles in the “weather” section AND “sydney” category:
<txp:article_custom form="headline" section="weather" category="sydney" />

Articles in the “local” category, starting with the oldest:
<txp:article_custom form="headline" category="local" sortdir="asc" />

Articles from a particular month:
<txp:article_custom form="headline" month="2003-11" />

Articles from a given day:
<txp:article_custom form="headline" month="2003-11-25" />

Articles by a particular author:
<txp:article_custom form="headline" author="jim" />

Show 10 recent articles, starting with the second:
<txp:article_custom form="headline" limit="1,10" />
(the first record is number 0; to start with the fifth article, use limit="4,10")

5 random articles:
<txp:article_custom form="headline" limit=5 sortby="rand()" />

Most recently modified articles:
<txp:article custom form="headline" sortby="LastMod" />

To change the appearance of the list, or add extra information to each item, edit the headline form. Some examples:

Include the Excerpt field with each headline

<li><txp:permlink><txp:title /></txp:permlink></li>
<li><txp:excerpt /></li>

Show article dates

<li><txp_posted format="Y-m-d" />: <txp:permlink><txp:title /></txp:permlink></li>

Use line breaks instead of list formatting:

<txp:permlink><txp:title /></txp:permlink></br />

(you’ll need to edit your page code to remove the <ul> </ul>, of course)

Include the article thumbnail beside each title (requires the zem_article_image plugin):

<li><txp:zem_article_thumb />
<txp:permlink><txp:title /></txp:permlink></li>

Show the category name:

<li><txp:category1 /> - <txp:permlink><txp:title /></txp:permlink></li>

Show the author name:

<li><txp:permlink><txp:title /></txp:permlink> (<txp:author />)</li>

Feel free to suggest additional examples in comments.


Hi,

How would I produce this on an FAQs section page…

Category 1
> Links to
> all of the
> articles in
> Category 1
> in asc by alpha

Category 2
> Same
> as above

Then when I click one of the links it takes me to the article for that link.

Vaughn Taylor    Aug 19, 12:49 am    #

Vaughn,

If the number of categories are small and static, you could just list them explicitly, like this:

< h2 >Category 1< /h2 >
< txp:article_custom form="headline" category="category_1" />
< h2 >Category 2< /h2 >
< txp:article_custom form="headline" category="category_2" />

..etc.

zem    Aug 19, 12:10 pm    #

this is amazing. i could have gone to sleep last night had i only found this earlier (or at least i could have got more done)

being so green to this that i’m almost yellow, i’m wonderig if there’s a way to group posts on multiple levels. for instance, grouping posts by day (for the ‘more than one a day’ instance) and then grouping them by month, but without having to “hard code” the month.

so my list could look like:
[ul]
[li]25 OCT 2004
[ul]
[li]post title five[/li]
[li]post title four[/li]
[/ul][/li]

[li]23 OCT 2004
[ul]
[li]post title three[/li]
[/ul][/li]

[li]19 OCT 2004
[ul]
[li]post title two[/li]
[li]post title one[/li]
[/ul][/li]
[/ul]

and then have an archive link to a more robust list spanning more than the current month.

thanks for your time.

jase    Oct 25, 09:07 pm    #

000000000000

— 0000000    Jan 19, 12:22 pm    #

This is wonderful. Thank you for writing this up. I realize now how little I really know about Textpattern.

Jenifer    Apr 29, 12:12 pm    #

Thanks, this is great. Is it possible to list categories by current section, so I wouldn´t have to change code for every page and listings would be dynamic?

— Topi    Apr 29, 10:40 pm    #

I was going over this article trying to get it implemented on my site and calling a link to a perminant article in a certain category and this is what i get when clicking on the link:

Fatal error: Call to undefined function: lookupbyid() in /home/brewerde/public_html/textpattern/publish.php(769) : eval()’d code on line 1814

I am not sure where to go to try and fix this… any help would be really appreciated.

Thanks,

Joshua

Joshua Brewer    May 3, 03:13 am    #

is it possible to sort by the custom1 field?

— James    May 31, 09:10 am    #

the list above is VERY helpful, thank you very much.

i found, then, that:
A: in the show article dates paragraph there is a typo.
:
shoudl read
:

B:the limit=”4,99999” in an article custom tag didn’t work here in textpattern 4.0.3.
probably my fault? dunno. will find out.

and a newbie quest?
i wonder how to EXCLUDE results from an article custom display. say i don’t want to see articles of the current month, how would i do that? is there an exclusion tag? i am sorry if i posted a too dumb question and: yes, i am a newbie to textpattern.

thanx mucho for your work & hints, zem.

jayrope    Jul 16, 08:27 am    #

this article is very helpful. i was trying to create lists with articles from different sections, including excerpts. thank you.

issa    Jan 8, 12:38 pm    #

Thanks for this! I’m brand new to TextPattern and this really helped out with one of the essential parts of my website!!!

Qwest    Jan 28, 08:31 pm    #

I too am interested in jayrope’s question — how to exclude a category from an article_custom list. Is there a solution to this?

Mike Ruddy    May 12, 07:00 am    #

Commenting is closed for this article.