Get first image from page content?
-
kr428
- Sr. Bludit
- Posts: 45
- Joined: Thu Jan 05, 2023 9:07 pm
- Has thanked: 14 times
- Been thanked: 4 times
Folks,
for a photoblog-like scenario, I want to build a theme which, on the start page, displays a list of images linking to the corresponding post (which includes a short text and hashtags). My idea, for that, would be to modify this call...
<?php echo $page->content() ?>
... in the template to not just use the content() but actually get the first <img> tag from the content and ignore everything else.
Is that possible? More specifically, what structure does content() return? Especially given I'm using EasyMDE and so most likely markdown for editing pages?
Thanks very much and best regards,
Kristian
for a photoblog-like scenario, I want to build a theme which, on the start page, displays a list of images linking to the corresponding post (which includes a short text and hashtags). My idea, for that, would be to modify this call...
<?php echo $page->content() ?>
... in the template to not just use the content() but actually get the first <img> tag from the content and ignore everything else.
Is that possible? More specifically, what structure does content() return? Especially given I'm using EasyMDE and so most likely markdown for editing pages?
Thanks very much and best regards,
Kristian
-
arfa
- Master Bludit
- Posts: 163
- Joined: Tue Jul 04, 2017 4:31 am
- Location: New Zealand
- Has thanked: 13 times
- Been thanked: 27 times
Sounds intriguing.
The first thought that arose was that you need a regex. I have used these but always have to do some looking/asking to refine the expression - start with AI?
This is a quick (untested) try where $match[1] holds all <img tags as an array. $match[1][0] should be your first image. You would probably need a code feed in for that...
I just posted one "addCode2Page" but my learning curve (think it is my metadata) throws errors on https://plugins.bludit.com/
First build a non-bludit page with php and experiment with a regex.
Post in bludit
try my plugin - direct download
https://bodhinyanarama.net.nz/temp/addCode2Page.zip
I haven't tried it with regex but it is just php so should not be a problem. I am tight on time but will give the above suggestion some trial. Can you confirm that you want the first img tag?
You did say you want a list of images - the regex will also do that; just loop $match
Links to pages is another Q for another time - first $match?
kusalo
The first thought that arose was that you need a regex. I have used these but always have to do some looking/asking to refine the expression - start with AI?
Code: Select all
$imgTags = preg_match_all("#<img(.+?)</img#",$content_is,$match);I just posted one "addCode2Page" but my learning curve (think it is my metadata) throws errors on https://plugins.bludit.com/
First build a non-bludit page with php and experiment with a regex.
Post in bludit
try my plugin - direct download
https://bodhinyanarama.net.nz/temp/addCode2Page.zip
I haven't tried it with regex but it is just php so should not be a problem. I am tight on time but will give the above suggestion some trial. Can you confirm that you want the first img tag?
You did say you want a list of images - the regex will also do that; just loop $match
Links to pages is another Q for another time - first $match?
kusalo

