Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2011-12-15 07:56:41

JeroenC
Member
From: Finland
Registered: 2010-04-14
Posts: 30
Website

display 3 articles based on custom field value

Hello all,

I work with TXP way too little to solve this issue myself. I went through this forum using the “Search” tool but could not find the answer there either, nor on the FAQ. So now a question for the smart people out here.

At the homepage (draft version) I want to list 3 articles which talk about theatre plays, see link

  1. There is a play planned in the future
  2. There is a current play
  3. And 1 from the past
    plus a link to all plays (which I manage)
I have a custom field named “when” And use the values:
  1. future
  2. current
  3. past

My code is
<txp:article_custom section="esitys" category="aulanko" />

<txp:if_custom_field name="when" value="future">
<div class="article articleFirst">
<h2><txp:permlink><txp:title /></txp:permlink></h2><br>
<p><txp:body /></p>
</div>
</txp:if_custom_field>

<txp:if_custom_field name="when" value="current">
<div class="article articleFirst">
<h2><txp:permlink><txp:title /></txp:permlink></h2><br>
<p><txp:body /></p>
</div>
</txp:if_custom_field>

<txp:if_custom_field name="when" value="past">
<div class="article articleFirst">
<h2><txp:permlink><txp:title /></txp:permlink></h2><br>
<p><txp:body /></p>
</div></txp:if_custom_field>

</txp:article_custom>

And the result can be found here

Any ideas?

Thanks already,
Jeroen

Last edited by JeroenC (2011-12-15 08:03:09)


Time to volunteer? Come and join us at a great project which is going to be opensource: https://www.caloom.com

Offline

#2 2011-12-15 09:26:43

milosevic
Member
From: Madrid, Spain
Registered: 2005-09-19
Posts: 390

Re: display 3 articles based on custom field value

You can manage the time without using custom fields.

When publish an article, give it an publish date (the date when starts the play) and an expiration date (when the representations ends).

Then, you can recover articles by time concept:

<txp:article_custom time="future" limit="1"/>
<txp:article_custom limit="1" />
<txp:article_custom time="past" limit="1"/>

The first line recovers the first future play
The second line recovers the most recent article alive (the most recent article dated since present to past will be your current play)
The thirth line recovers the most recent past play

And remember that, when using this method, if you want to list all plays in other part of the site, ad the param time=“any” to article_custom / article in order to recover both the past and future articles, otherwise the future articles won’t be listed.

Last edited by milosevic (2011-12-15 09:34:43)


<txp:rocks/>

Offline

#3 2011-12-15 09:34:50

JeroenC
Member
From: Finland
Registered: 2010-04-14
Posts: 30
Website

Re: display 3 articles based on custom field value

Thanks, this looks very elegant. I wasn’t aware of the future and past value.
I do this site for a local children theatre and thought adding simple tags per article with a custom field would be simple, but your solution is way more simple. Thanks again. I give it a try


Time to volunteer? Come and join us at a great project which is going to be opensource: https://www.caloom.com

Offline

#4 2011-12-15 09:42:44

milosevic
Member
From: Madrid, Spain
Registered: 2005-09-19
Posts: 390

Re: display 3 articles based on custom field value

You are wellcome.

The code solution is only theory, I use this in other sites but haven’t try the exact code for you. Perhaps you will need to play arround with the “sort” param to recover the exact first future and last past plays:

<txp:article_custom time="future" limit="1" sort="Posted asc"/>
<txp:article_custom limit="1" />
<txp:article_custom time="past" limit="1" sort="Posted desc"/>

asc / desc …I’m not sure, try it.


<txp:rocks/>

Offline

#5 2011-12-15 09:43:29

JeroenC
Member
From: Finland
Registered: 2010-04-14
Posts: 30
Website

Re: display 3 articles based on custom field value

In my case what is wrong with this code then (will try the sort tag)

<txp:article_custom section="esitys" category="aulanko" time="future" limit="1"/>
<div class="article articleFirst">
<h2><txp:permlink><txp:title /></txp:permlink></h2><br>
<p><txp:body /></p>
</div>
</txp:article_custom>

<txp:article_custom section="esitys" category="aulanko" limit="1" />
<div class="article articleFirst">
<h2><txp:permlink><txp:title /></txp:permlink></h2><br>
<p><txp:body /></p>
</div>
</txp:article_custom>

<txp:article_custom section="esitys" category="aulanko" time="past" limit="1"/>
<div class="article articleFirst">
<h2><txp:permlink><txp:title /></txp:permlink></h2><br>
<p><txp:body /></p>
</div>
</txp:article_custom>

{Added bc.. for better code display. -Uli}

Last edited by uli (2011-12-15 11:36:07)


Time to volunteer? Come and join us at a great project which is going to be opensource: https://www.caloom.com

Offline

#6 2011-12-15 12:24:33

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

Re: display 3 articles based on custom field value

All the <txp:article_custom /> in your code are self-closed, but you are trying to use them in the container form (<txp:article_custom>...</txp:article_custom>. Try remove the closing slash (/) on each opening <txp:article_custom> tag.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#7 2011-12-15 13:25:50

JeroenC
Member
From: Finland
Registered: 2010-04-14
Posts: 30
Website

Re: display 3 articles based on custom field value

Thank you Julián, how could I be so blind (again).

I am getting there but not quiet yet. Only 2 of the 3 articles are displayed. Screenshot of result

my code is:

<txp:article_custom section="esitys" category="aulanko" time="future" limit="1" >
<div class="article articleFirst">
<h2><txp:permlink><txp:title /></txp:permlink></h2><br>
<p><txp:body /></p>
</div>
</txp:article_custom>

<txp:article_custom section="esitys" category="aulanko" limit="1"  >
<div class="article articleFirst">
<h2><txp:permlink><txp:title /></txp:permlink></h2><br>
<p><txp:body /></p>
</div>
</txp:article_custom>

<txp:article_custom section="esitys" category="aulanko" time="past" limit="1" >
<div class="article articleFirst">
<h2><txp:permlink><txp:title /></txp:permlink></h2><br>
<p><txp:body /></p>
</div>
</txp:article_custom>

and the 3 articles have the following properties:
title: future
section: esitys
category: aulanko
Date: 2012 / 06 / 01

title: current
section: esitys
category: aulanko
Date: 2011 / 12 / 15

title: past
section: esitys
category: aulanko
Date: 2010 / 06 / 01

Anybody an idea what I am missing here?


Time to volunteer? Come and join us at a great project which is going to be opensource: https://www.caloom.com

Offline

#8 2011-12-15 13:44:28

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,315

Re: display 3 articles based on custom field value

I can’t tell the exact reason why it isn’t working, but looking for faults in the current stage seems not the right step (Ooh, sounds so rigorous ;). You’ll need to start adding sort attributes, add two one more articles (1 future, 1 past) in order to be sure sorting works, and also use the limit/offset combo, as there is no present time. There’s future and past in TXP, and article_custom defaults to past.

Last edited by uli (2011-12-15 14:39:37)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#9 2011-12-15 14:45:53

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,315

Re: display 3 articles based on custom field value

uli wrote:

I can’t tell the exact reason why it isn’t working

Heck, error in reasoning: Like I said: article_custom defaults to past, and you’ve two of these tags limited to display just one article, the first one. Hope that’s it, finally ;)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#10 2011-12-15 14:52:34

milosevic
Member
From: Madrid, Spain
Registered: 2005-09-19
Posts: 390

Re: display 3 articles based on custom field value

Mmmm. I think my theory is wrong.

article_custom only detects Past an Future articles, but no “present” articles (default value is “Past”).

If the Theatre only works a play at sametime, try this:

<the future play>

<the most recent past play (=current play)>

<the most recent past play minus 1 (using offset=“1”)>

this way:

<txp:article_custom section="esitys" category="aulanko" time="future" limit="1" >
...
</txp:article_custom>
<txp:article_custom section="esitys" category="aulanko" limit="1"  >
...
</txp:article_custom>
<txp:article_custom section="esitys" category="aulanko" limit="1"  offset="1">
...
</txp:article_custom>

Last edited by milosevic (2011-12-15 14:53:34)


<txp:rocks/>

Offline

#11 2011-12-15 15:07:48

JeroenC
Member
From: Finland
Registered: 2010-04-14
Posts: 30
Website

Re: display 3 articles based on custom field value

Danke Uli,

yes it works now, great!
Up to the next challenge :-)


Time to volunteer? Come and join us at a great project which is going to be opensource: https://www.caloom.com

Offline

Board footer

Powered by FluxBB