Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2024-06-26 12:30:59

Gallex
Member
Registered: 2006-10-08
Posts: 1,313

if article image else...

hi!

if ithere is article image -display it, if not – display random image and direct it to an article. that is my plan.

my code:

<txp:if_article_image>
<txp:permlink><txp:article_image /></txp:permlink>
<txp:else />
<txp:permlink><txp:images id="23,18" limit="1" sort="rand()" /></txp:permlink>
</txp:if_article_image>

it displays a random image if article image field is empty, but the permlink doesn’t work, it doesn’t direct to an article

here

Offline

#2 2024-06-27 03:55:56

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,132
Website GitHub Mastodon Twitter

Re: if article image else...

Gallex wrote #337343:

it displays a random image if article image field is empty, but the permlink doesn’t work, it doesn’t direct to an article

That is because the images do not contain article info. The way I would go about it is to add the article id in the image name field of the image, so if you want a particular image to link to a particular article_id your code could become

<txp:if_article_image>
<txp:permlink><txp:article_image /></txp:permlink>
<txp:else />
<txp:images id="23,18" limit="1" sort="rand()">
<txp:permlink id="<txp:image_name />"><txp:image /></txp:permlink>
</txp:images>
</txp:if_article_image>

Edited to add another code which is possibly closer to what you seek.

<txp:permlink>
<txp:if_article_image>
<txp:article_image />
<txp:else />
<txp:images id="23,18" limit="1" sort="rand()">
<txp:image />
</txp:images>
</txp:if_article_image>
</txp:permlink>

Last edited by colak (2024-06-27 05:07:28)


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#3 2024-06-27 05:26:39

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

Re: if article image else...

Yiannis’ suggestion of equipping each image with a corresponding article id so you can link an image back to a specific article would work. But it means you need to manually add extra information to your images.

The problem may lie elsewhere. The permlink seems to go to a subpage of an image category list, and in your page source, you have two nested <a> tags, one for the article and another around the image. This stems from using txp:images without a corresponding form or a contained tag. The docs for txp:image says that if no form is specified and there are no contained tags, it produces images for use with image_display and that is producing the second nested link.

Try using <txp:images …><txp:image /></txp:images>. You can also wrap the entire block in a txp:permlink, e.g.

<txp:permlink>
    <txp:if_article_image>
        <txp:article_image />
    <txp:else />
        <txp:images id="23,18" limit="1" sort="rand()"><txp:image /></txp:images>
    </txp:if_article_image> 
</txp:permlink>

That should resolve it for you without inputting additional links.


TXP Builders – finely-crafted code, design and txp

Online

#4 2024-06-27 08:36:47

Gallex
Member
Registered: 2006-10-08
Posts: 1,313

Re: if article image else...

Perfect! Thank you guys!

Offline

Board footer

Powered by FluxBB