Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2011-03-08 16:32:57

caruso_g
Member
From: Catania
Registered: 2009-04-11
Posts: 88
Website

How to get the second article image only?

Hi guys, I need to get only the second article image, but with this code:

<txp:images offset="1" wraptag="div" class="header_image" auto_detect="article" />

I got this HTML:

<div class="header_image">
    <a href="http://mywebsite.com/index.php?c=site-design&amp;context=image&amp;s=product_name&amp;p=14">
        <img src="http://mywebsite.com/images/14t.png" alt="Product icon">
    </a>
</div>

Which, instead, is the first image. Btw, I also just want the full image, I don’t want the thumbnail and I don’t want the link to the full size image.

I also tried :

<txp:images offset="1" wraptag="div" class="header_image" auto_detect="article">
    <txp:image />
</txp:images>

but it still shows me just the first article image… argh.
Where’s the trick? I really don’t see it. Thank you all for your help!

Offline

#2 2011-03-08 16:48:33

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

Re: How to get the second article image only?

Very close. How about this for a trick:

<txp:images offset="1" limit="1" wraptag="div"
     class="header_image"
     sort='FIELD(ID,<txp:custom_field name="article_image" />)'>
   <txp:image />
</txp:images>

To trigger the offset you need a limit (any limit will do) and to make sure that the tag keeps the order of the IDs as you specified in the article image field you need the sort hack to retrieve the raw list of images and then supply that to MySQL.

Last edited by Bloke (2011-03-08 16:48:58)


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

#3 2011-03-08 16:55:21

caruso_g
Member
From: Catania
Registered: 2009-04-11
Posts: 88
Website

Re: How to get the second article image only?

OK, it seems I got it working. My fault, of course.
Offset is desc so last image added (in fact the second one I needed) was skipped, and first one was displayed.
Right code is:

<txp:images wraptag="div" class="header_image" auto_detect="article" limit="1">
    <txp:image />
</txp:images>

Sorry if I made you waste your time. I’ll left it here as reference if someone else should get the same problem.

More, for reference as well, what if I had more then two images? How could I get the second one only if I have three images?
Simple enough:

<txp:images wraptag="div" class="header_image" auto_detect="article" offset="1" limit="1">
    <txp:image />
</txp:images>

Thank you all anyway! :)

Offline

#4 2011-03-08 17:03:27

caruso_g
Member
From: Catania
Registered: 2009-04-11
Posts: 88
Website

Re: How to get the second article image only?

Bloke wrote:

Very close. How about this for a trick:

<txp:images offset="1" limit="1" wraptag="div"
     class="header_image"
     sort='FIELD(ID,<txp:custom_field name="article_image" />)'>
   <txp:image />
</txp:images>

To trigger the offset you need a limit (any limit will do) and to make sure that the tag keeps the order of the IDs as you specified in the article image field you need the sort hack to retrieve the raw list of images and then supply that to MySQL.

Hi Stef! Thanks a lot, really interesting trick. The issue is that I want to limit as much as possible the use of custom fields (and I already used a lot!) to the client. Anyway, thanks again, you cleared me how the tag works.

o.t. Take a look here
What about that feature request? :)

Offline

#5 2011-03-08 17:12:16

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

Re: How to get the second article image only?

caruso_g wrote:

Offset is desc so last image added (in fact the second one I needed) was skipped, and first one was displayed.

Sure. But in case anyone is wondering why the sort popped up in my version, you’ll need it if you (or your client) put your images in a non-filename order inside the article image field.

Example: Assuming your articles were named this:

5: a.jpg
8: b.jpg
42: c.jpg
90: d.jpg

with article_image = 5, 42, 90, 8 your version will return image 8 (‘b’ is the second image in the list, i.e. at offset=1 when ordered a.jpg, b.jpg, c.jpg, d.jpg), but mine with the sort will return image 42 because it forces the order to remain as you put it in the article image. If the file names are in the same order alphabetically as the images are sequentially it makes this behaviour seem a little wonky if later on a new images is added (with a higher ID) that has a lower alphabetic name!

I want to limit as much as possible the use of custom fields

Look closer. It’s not a custom field, it’s the article image field which is retrived via the custom_field tag :-)


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

Board footer

Powered by FluxBB