Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
[Solved] Displaying a random article but not the current one
Hi TXPexperts! 😍
Because my client do not need categories, currently, I’m using with some good results in order to simulate related_article
😜:
<txp:variable name="ID" value='<txp:article_id />' />
<txp:article_custom section='<txp:section />' limit="1" sort="rand()">
<txp:evaluate query='contains(<txp:variable name="ID" />,<txp:article_id />)'><txp:else />
<txp:title />
</txp:evaluate>
</txp:article_custom>
I wonder if there is a kind of negative attribute in order to simplify my purpose (i.e. not='<txp:article_id />'
)?
Please, don’t blame me if I’m not clever: I just discover this very useful <txp:article />
tag! 😁
Last edited by Pat64 (2020-12-13 12:51:07)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: [Solved] Displaying a random article but not the current one
Hi Patrick,
there are many solutions, the most straightforward is to use empty match
attribute:
<txp:related_articles section match="" limit="1" sort="rand()">
<txp:title />
</txp:related_articles>
Offline
Re: [Solved] Displaying a random article but not the current one
Pat64 wrote #327434:
I wonder if there is a kind of negative attribute in order to simplify my purpose…
Hi PatTXPexpert 😄
There is! It’s called exclude
. See the attribute description in the docs. You should be able to do:
<txp:article_custom section='<txp:section />' exclude='<txp:article_id />' limit="1" sort="rand()">
<txp:title />
</txp:article_custom>
TXP Builders – finely-crafted code, design and txp
Offline
Re: [Solved] Displaying a random article but not the current one
My bad!
Obviously, Julian, there is an exclude
attribute…
But, sorry, I prefer Oleg’s solution 😜
(in use, with some good results, here: https://editions-du-26-octobre.com/catalogue-general/le-collage-paul-alexis)
Last edited by Pat64 (2020-12-12 13:17:33)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: [Solved] Displaying a random article but not the current one
Pat64 wrote #327437:
I prefer Oleg’s solution 😜
Internally it is mapped to Jacob’s one :-)
Will be even more straightforward in 4.8.5:
<txp:related_articles match="section" limit="1" sort="rand()">
<txp:title />
</txp:related_articles>
Offline
Re: [Solved] Displaying a random article but not the current one
Cool, Thanks !
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: [Solved] Displaying a random article but not the current one
Oleg:
Is there a way to filter the results by empty custom field (or negative)?
We can’t add this attribute custom_field_name="%"
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: [Solved] Displaying a random article but not the current one
Would this help?
<txp:related_articles match="section" limit="1" sort="rand()">
<txp:if_custom_field name="name" not>
<txp:if_custom_field name="name2" not>
<txp:if_custom_field name="name3" not>
<txp:if_custom_field name="name4" not>
<txp:if_custom_field name="name5" not>
<txp:title />
</txp:if_custom_field>
</txp:if_custom_field>
</txp:if_custom_field>
</txp:if_custom_field>
</txp:if_custom_field>
</txp:related_articles>
Edited to add that it would be nice if a comma separated list of CF names could be supported. Something like… <txp:if_custom_field name="name,name2,name_etc" not>
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: [Solved] Displaying a random article but not the current one
Thank you Yiannis.
But, unfortunately, sometimes we got no results (matching custom fields are omitted)…
Just to notice: my first error comes with the official document where “related articles tag is used to produce a list of related (by category) articles” but in fact it is more flexible (see: https://docs.textpattern.com/tags/related_articles)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: [Solved] Displaying a random article but not the current one
Ok.
Based both on etc
and colak
help, I’m using this:
match=", not %my_custom_field%"
It seems to work as expected. But, now I need some Dev’s expertise: is it good or… bad? 🤨
Who’s bad
— Michael Jackson.
Last edited by Pat64 (2020-12-13 09:13:07)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: [Solved] Displaying a random article but not the current one
Pat64 wrote #327455:
match=", not %my_custom_field%"
It seems to work as expected. But, now I need some Dev’s expertise: is it good or… bad? 🤨
Iznogoud at all, at least match
was not intended to work like this. Awaiting 4.8.5 you should switch to jacob’s solution (untested):
<txp:article_custom section id cf_name exclude='id, cf_name' limit="1" sort="rand()">
<txp:title />
</txp:article_custom>
This should exclude the current article (id
) and all articles with non-empty cf_name
.
Offline
Re: [Solved] Displaying a random article but not the current one
😃
My solution doesn’t work at all. I kept it here to show how I’m a bad TXP user.
…And if I want to exclude all with empty cf_name
?
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline