Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-04-20 21:56:12

Logoleptic
Plugin Author
From: Kansas, USA
Registered: 2004-02-29
Posts: 482

Manually Sort txp:article_custom Output?

The wiki specifically says that the order in which you list IDs in this tag’s id attribute doesn’t imply a sort order, but I find myself in a position where I’d really like to manually sort specific articles. Does anyone know of a way to make this work?

The details: I need to output two selected article excerpts onto the page. Right now this is happening in a sidebar, but future design changes may require the two excerpts to be in completely different parts of the page. Getting this far is easy enough — put two article IDs into a custom field separated by a comma, and read them back out like so:

<txp:article_custom id='<txp:custom_field name="promo_article_ids" default="12,17" escape="" />' limit="2" section="promos" form="promo.excerpt" />

If I want to put the two excerpts in different places, I can just use two tags with a limit of 1 and and offset of 1 on the second tag. But what about choosing the order in which the excerpts appear, or (in the future) which excerpt goes into which spot on the page? Any suggestions?

Offline

#2 2010-04-20 22:46:01

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: Manually Sort txp:article_custom Output?

I wouldn’t be surprised if smd_each could do that.

Offline

#3 2010-04-20 23:07:15

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Manually Sort txp:article_custom Output?

Logoleptic wrote:

Any suggestions?

Smd_each is probably better, but you can also do it with like rah_repeat

With it you can split the list of IDs the way you want and represent them in different places on the page by using offset and limit, or if_last/first conditionals.

<!--
	First:
-->
<txp:rah_repeat limit="1" value='<txp:custom_field name="promo_article_ids" default="12,17" escape="" />'>
	<txp:article_custom id='<txp:rah_repeat_value />' form="promo.excerpt" />
</txp:rah_repeat>
<!--
Second:
-->
<txp:rah_repeat offset="1" limit="1" value='<txp:custom_field name="promo_article_ids" default="12,17" escape="" />'>
	<txp:article_custom id='<txp:rah_repeat_value />' form="promo.excerpt" />
</txp:rah_repeat>

In native method, with out any plugins, you could possibly say, sort the articles by values stored inside custom fields. So like for example you could assocate some values into the articles, and use the customfield as sort criteria. Tho, it’s more work.

The seperating of the articles could be done with article_custom’s offset, but if you want to save database queries (and possibly reserve speed as a side product) you could store the second article in <txp:variable> and call that later on.

With out the variable trick:

<!--
	First:
-->
<txp:article_custom sort="custom_1 asc" id="<txp:custom_field name="promo_article_ids" default="12,17" escape="" />" limit="1" form="promo.excerpt" />
<!--
	Second:
-->
<txp:article_custom sort="custom_1 asc" id="<txp:custom_field name="promo_article_ids" default="12,17" escape="" />" offset="1" limit="1" form="promo.excerpt" />

Last edited by Gocom (2010-04-20 23:11:13)

Offline

#4 2010-04-20 23:49:20

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: Manually Sort txp:article_custom Output?

hi Adam,

you could also try this trick:

<txp:article_custom id="5,1,4,2,3" sort="FIELD(ID,5,1,4,2,3)" />

Or, in your snippet:

<txp:article_custom id='<txp:custom_field name="promo_article_ids" default="12,17" escape="" />' limit="2" section="promos" form="promo.excerpt" sort='FIELD(ID,<txp:custom_field name="promo_article_ids" default="12,17" escape="" />)' />

La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#5 2010-04-21 06:48:28

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: Manually Sort txp:article_custom Output?

maniqui wrote:

you could also try this trick

Oh, I didn’t know that! Thank you :)

Last edited by els (2010-04-21 06:48:45)

Offline

#6 2010-04-23 03:42:01

Logoleptic
Plugin Author
From: Kansas, USA
Registered: 2004-02-29
Posts: 482

Re: Manually Sort txp:article_custom Output?

Thanks for the help, everyone. Julián’s tip looks like just what I needed for sorting, but I’ll have to keep smd_each in mind for later. It kind of makes me think of wet_for_each_image on steroids. (That’s a good thing.)

As for splitting the two excerpts up on the page, I remembered a few hours after posting that I wrote a plugin a couple of years ago specifically for wrangling delimited custom field data. Not sure how that managed to slip my mind.

Offline

Board footer

Powered by FluxBB