Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2022-10-25 00:25:36

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 595
Website

Get recent N images in category and show a random one?

Hi All,

I’m wondering about a good way to show a sidebar image like this: Get most recent 10 images in category X, then pick a random one from that group of 10, and show it.

I thought of some ideas involving direct PHP but wondered if there’s a more TXP-taggy way to do it. Thanks!

Offline

#2 2022-10-25 07:52:20

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,742
Website

Re: Get recent N images in category and show a random one?

You can do that in a two-step setup by getting the ids of the ten most recent images and then plugging them in to a second txp:images and choosing one random image. I can’t think of a way of doing that in one step with txp tags, though Oleg or Stef may have a trick up their sleeve(s), perhaps using a plugin like etc_query or smd_query.

<txp:variable name="recent_images" trim>
    <txp:images category="your-category" limit="10" break="," sort="id desc"><txp:image_info type="id" /></txp:images>
</txp:variable>

<txp:images id='<txp:variable name="recent_images" />' limit="1" sort="rand()">
    <txp:image />
</txp:images>

You could also use sort="date desc" if you prefer.


TXP Builders – finely-crafted code, design and txp

Offline

#3 2022-10-25 08:23:44

etc
Developer
Registered: 2010-11-11
Posts: 5,236
Website GitHub

Re: Get recent N images in category and show a random one?

I’d say it’s doable in one step and a half, avoiding two db calls. The idea is to separate the list of the 10 most recent images with something exotic, say #--. Then you can split this list by #-- and pick a random item via global txp 4.8.8 breakby, limit and sort attributes:

<txp:hide process="1" breakby="#--" sort="random" limit="1">
    <txp:images category="your-category" limit="10" break="#--" />
</txp:hide>

Offline

#4 2022-10-25 09:17:10

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,742
Website

Re: Get recent N images in category and show a random one?

Geniál !


TXP Builders – finely-crafted code, design and txp

Offline

#5 2022-10-25 17:01:33

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 595
Website

Re: Get recent N images in category and show a random one?

Thanks Jakob and Oleg!

This seems to work well:

<txp:hide process="1" breakby="#--" sort="random" limit="1">
        <txp:images category="sidebar-highlights" class="imgs" limit="12" sort="id desc" break="#--" />
    </txp:hide><!-- Not your normal use of t:h -->

The image does link to some kind of image context though. /?c=sidebar-highlights&context=image&p=704

Until I can handle that gracefully, what’s a good way to remove the link?

Last edited by maruchan (2022-10-25 17:02:09)

Offline

#6 2022-10-25 20:56:39

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,468
Website GitHub

Re: Get recent N images in category and show a random one?

Without any form or container content, <txp:images /> will output a context-sensitive image link, as you noted.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#7 2022-10-26 07:32:23

etc
Developer
Registered: 2010-11-11
Posts: 5,236
Website GitHub

Re: Get recent N images in category and show a random one?

jakob wrote #334018:

Geniál !

Gracias, but there is absolutely no guarantee it is any faster then your solution, SQL servers are performant nowadays. I think the best way is a mix of both:

<txp:variable name="randimg" breakby="," sort="random" limit="1">
    <txp:images category="sidebar-highlights" limit="12" sort="id desc" break=",">
        <txp:image_info type="id" />
    </txp:images>
</txp:variable>

<txp:image id='<txp:variable name="randimg" />' class="imgs" />

There will be no second db call in <txp:image />, since images data is cached by the first <txp:images /> tag.

Offline

#8 2022-10-26 13:28:53

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,742
Website

Re: Get recent N images in category and show a random one?

etc wrote #334028:

Gracias, but there is absolutely no guarantee it is any faster then your solution, SQL servers are performant nowadays. I think the best way is a mix of both:

<txp:variable name="randimg" breakby="," sort="random" limit="1">...

There will be no second db call in <txp:image />, since images data is cached by the first <txp:images /> tag.

I’ve not tried it, but that’s also a nice solution. If I’ve understood it properly, the outer txp:variable statement is breaking the comma-separated list into its individual values and randomly picking one?

And for this the attribute value should be random and not rand() like in txp:article(_custom)?


TXP Builders – finely-crafted code, design and txp

Offline

#9 2022-10-27 10:29:50

etc
Developer
Registered: 2010-11-11
Posts: 5,236
Website GitHub

Re: Get recent N images in category and show a random one?

jakob wrote #334030:

… the outer txp:variable statement is breaking the comma-separated list into its individual values and randomly picking one?

And for this the attribute value should be random and not rand() like in txp:article(_custom)?

Yes, here sort is a global txp attribute, that can (jointly with global breakby and limit) split, sort and limit the output of any tag… unless the tag has its own sort attribute. This is the case of <txp:article />, where sorting is done by the tag itself at db level, say, via SQL rand() function. Other tags could use another (not SQL) sorting mechanism, so the global sort has not adopted a specific rand() as one of its values, but replaced it with a generic random.

Offline

#10 2022-10-27 20:02:23

etc
Developer
Registered: 2010-11-11
Posts: 5,236
Website GitHub

Re: Get recent N images in category and show a random one?

Even better, but requires at least 10 images:

<txp:variable name="randoffset" value="0,1,2,3,4,5,6,7,8,9" breakby="," sort="random" limit="1" />

<txp:images limit="1" offset='<txp:variable name="randoffset" />' sort="id desc">
    <txp:image />
</txp:images>

Offline

Board footer

Powered by FluxBB