Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2021-06-17 15:41:55

hilaryaq
Plugin Author
Registered: 2006-08-20
Posts: 335
Website

How to get article img url of first article

Hi all!

I use forms for single pages because I’m weird like that hah! So I’d like to put the article image url into the meta head for og:image, but of course because it’s an article list page technically speaking, I can’t get it.

Is there a way to get the article image url from a page from the first article listed on the page?

Many thanks!


…………………
I <3 txp
…………………

Offline

#2 2021-06-17 16:08:51

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

Re: How to get article img url of first article

hilaryaq wrote #330547:

because it’s an article list page technically speaking, I can’t get it.

Is there a way to get the article image url from a page from the first article listed on the page?

Hi Hilary,

if the problem is only there, does <txp:if_first_article /> help?

<!-- head -->
...<txp:variable[-1] name="first_image" />...
<!-- body -->
...
<txp:article>
    <txp:if_first_article>
        <txp:variable name="first_article"><!-- retrieve image url here --></txp:variable>
    </txp:if_first_article>
</txp:article>

Offline

#3 2021-06-17 16:30:20

hilaryaq
Plugin Author
Registered: 2006-08-20
Posts: 335
Website

Re: How to get article img url of first article

Doesn’t seem to work :/ I don’t know if it’s possible, on individual articles it’s fine, but it’s just if a more static style page is shared out that only has one article on it, but the article does have an image, be very handy to get the url of it!


…………………
I <3 txp
…………………

Offline

#4 2021-06-17 16:33:08

hilaryaq
Plugin Author
Registered: 2006-08-20
Posts: 335
Website

Re: How to get article img url of first article

etc wrote #330548:

Hi Hilary,

if the problem is only there, does <txp:if_first_article /> help?

<!-- head -->...

Basically I want to output it in the head as a meta, current code is below, thanks for your help!

<meta property=“og:image” content=”<txp:if_article_image><txp:images limit=“1”><txp:image_url /></txp:images></txp:if_article_image>” />


…………………
I <3 txp
…………………

Offline

#5 2021-06-17 17:15:28

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

Re: How to get article img url of first article

Hi hillary

By first article do you mean the oldest article? if so here is a possible solution.

<txp:article_custom limit="1" sort="Posted asc">
<txp:variable name="imgmain"><txp:images break="" id='<txp:custom_field name="article_image" />'><txp:image_info type="id" /></txp:images></txp:variable>
<meta property="og:image" content="<txp:images break="" id='<txp:variable name="imgmain" />'><txp:image_url /></txp:images>" />
<meta property="og:image:width" content="<txp:images break="" id='<txp:variable name="imgmain" />'><txp:image_info type="w" /></txp:images>" />
<meta property="og:image:height" content="<txp:images break="" id='<txp:variable name="imgmain" />'><txp:image_info type="h" /></txp:images>" />
<meta property="og:image:alt" content="<txp:images break="" id='<txp:variable name="imgmain" />'><txp:image_info type="alt" /></txp:images>" />
</txp:if_custom_field>
</txp:article_custom>

if by first article you mean the latest article change it to <txp:article_custom limit="1">...


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

Offline

#6 2021-06-17 17:36:19

hilaryaq
Plugin Author
Registered: 2006-08-20
Posts: 335
Website

Re: How to get article img url of first article

Thanks Yiannis, it’s how to get the article image from a single article on a page (eg. static about page) outside of an article content in the head.

The most convoluted thing ever apologies!! :D


…………………
I <3 txp
…………………

Offline

#7 2021-06-17 17:49:29

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

Re: How to get article img url of first article

hilaryaq wrote #330552:

Thanks Yiannis, it’s how to get the article image from a single article on a page (eg. static about page) outside of an article content in the head.

There is no real problem to wrap the whole page in <txp:article />, if you are sure there is only one:

<txp:article limit="1">
<html>
<!-- the complete page code, excluding txp:article -->
</html>
</txp:article>

Offline

#8 2021-06-17 19:15:50

hilaryaq
Plugin Author
Registered: 2006-08-20
Posts: 335
Website

Re: How to get article img url of first article

etc wrote #330553:

There is no real problem to wrap the whole page in <txp:article />, if you are sure there is only one:

<txp:article limit="1">...

Eeep! Will give that a test tomorrow thanks etc! :)


…………………
I <3 txp
…………………

Offline

#9 2021-06-17 20:50:00

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

Re: How to get article img url of first article

etc wrote #330553:

There is no real problem to wrap the whole page in <txp:article />

I’ve done this before on a site, for precisely the same reason that I needed to populate metadata outside the normal article wrapper. Works a treat.


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

#10 2021-06-25 13:37:54

hilaryaq
Plugin Author
Registered: 2006-08-20
Posts: 335
Website

Re: How to get article img url of first article

So, I solved this with a little trick Stef taught me for another issue, by using a variable in my page_single form which I use to override the form and output single pages like about,contact etc

In my page_single form:


<txp:variable name="singlepage_img" value='<txp:if_article_image><txp:images><txp:image_url thumbnail="1" /></txp:images></txp:if_article_image>' />

In presentation > page (then my default page)


<txp:hide process="2"><meta property="og:image" content="<txp:variable name='singlepage_img' output />" /></txp:hide>

I feel I’m close to being able to use variables to fully control single pages, by just overriding the form to page_single which is such a maintainable way of doing things I feel. I like wrapping my single page mark up with article tags etc so it’s always been a handy way to operate within txp. I can use a variable to check whether the custom field has been over-ridden to page_single, and if it has, use variables to access those ‘article’ tags I need.

Could be a good path towards simple single page publishing!

I was always nervous about variables in head etc for some reason, in case they’d be blocked or something but I don’t think I need to be at all, they’re great. No different to outputting anything else with a txp tag!

Hilary


…………………
I <3 txp
…………………

Offline

Board footer

Powered by FluxBB