1
I found this script for wordpress, to grab the image of the post and add it in the wordpress theme.
View the script:
function catch_that_image() {
global $post, $posts;
$first_img = '';
$new_img_tag = "";
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post- >post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image with 0 width
$new_img_tag = "<img src='/images/noimage.jpg' width='0px' class='alignleft' />";
}
else{
$new_img_tag = "<img src='" . $first_img . "' width='100px' height='100px' class='alignleft' />";
}
return $new_img_tag;
}
end of script
This little code we added to call the image in some theme location.
<?php echo catch_that_image() ?>
As you can see the script leaves the image sized in 100x100, I would like to control the image size by this code :
<?php echo catch_that_image() ?>
I need to leave the image on each page with different dimensions.
tested here and it worked! Could you add the Alt and Title tag to the image? using <?php the_title() as a base? > from wordpress? t
– Endou
Probably. See the edited answer, @user3192159. I haven’t tried it, but it should work fine
– Caio Felipe Pereira
The alt code seems to have some error, it is not showing the title only a part of the title, example: Title blue cars, in the alt tag it appears alt="blue cars" src=".
– Endou
@user3192159 put a
echo
before theget_the_title()
. I edited the answer– Caio Felipe Pereira
when adding echo before get_the_title() the code shows an error.
– Endou
Now that I’ve touched. Since you have the global
$post
, you can give aecho
right in the title.echo $post->post_title;
– Caio Felipe Pereira
Let’s go continue this discussion in chat.
– Caio Felipe Pereira
Hello caio, blz, I found an error in the script, I had not noticed because I had not seen all the pages of the site. It turns out that articles that have an iframe or youtube embed, the script instead of taking the image of the article, it’s taking the link from the iframe src="//www.youtube.com/get_player" would not have the strength of the script to take only images Jpeg, jpg, png, gif of the articles and ignore iframe?
– Endou