Geting the first image url from a post? - php

I am trying to create a dynamic website, I have a database with some news, and I wanted to get the first image from the post content
The news are added to this database with wordpress, as you know wordpress images are inside the post_content.
Please can you help me to get the first image of the post and dispay the link.
Thank you

Put this in your functions.php file:
function get_first_image() {
global $post, $posts;
$first_img = '';
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
$first_img = "/images/default.jpg";
}
return $first_img;
}
and then simply call the function on the page you need to get the first image of the post like this:
<?php echo get_first_image() ?>

Related

How to catch alt content from wp post?

I would like to use alt attribute tags with image on the main site.
I use code that calls for pictures on my index page from latest wordpress posts. I use this code for index.php:
<img src="<?php echo catch_that_image() ?>" width="250" alt="">
The code in the functions.php file for catch that image is:
function catch_that_image() {
global $post, $posts;
$first_img = '';
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
$first_img = "/images/default.jpg";
}
return $first_img;
}
What I want to achieve is to get the alt tag from that post, not only the image. Actually some kind of "catch that alt". The new code in index.php would be
<img src="<?php echo catch_that_image() ?>" width="250" alt="text from wordpress post">
How can can I add anything to functions.php to get that alt text together?
The purpose is that google often search for images and doesn't find alt tags on the main site but only on individual wordpress posts.
you can add the following code in your functions.php to enable that.
function add_img_title($attr, $attachment = null) {
$img_title = trim(strip_tags($attachment->post_title));
$attr['alt'] = $img_title;
$attr['title'] = $img_title;
return $attr;
}
add_filter('wp_get_attachment_image_attributes', 'add_img_title', 10, 2);

Find image or iframe with regular expressions

I've got the following code, it spits out the first image of each post, on WordPress:
function catch_that_image() {
global $post, $posts;
$first_img = '';
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
}
echo "<img src=" . $first_img . ">";
}
However, I also need to catch the first iframe, and echo whichever is first. I'm not experienced with regular expressions, so any help or resources would be great :)
Use the |(or) operator. Replace the img with (img|iframe).

Add an ELSE to the IF in PHP script to get the default Image

I have this code inserted in the Function.PHP file of my wordpress this. What it basically do is:
when ever the user clicks the pinterest button, it ignores all the images in the blog post page but instead choose/return the feature image to be pinned.
function catch_that_image( $size = 'full' ) {
global $post;
if ( has_post_thumbnail($post->ID) ) {
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), $size);
return $featured_image[0];
}
return false;
}
I have no problem with the code above, but then I realized that what if there's no featured image?
Is it possible if someone can modify the code for to to add the following IF and Else Condition:
IF Featured Image is Present:
Run the script above. (I think it's covered in the above code and works fine)
BUT IF THERE'S NOT FEATURED IMAGE, choose/return a default image to be pinned.
I'm not really sure how to integrate this code (below) to above code. Sorry but i lack of knowledge in this area.
if(empty($first_img)){ //Defines a default image
$first_img = "http://www.mywebsite/wp-content/themes/Default_Image.jpg";
}
THank you verymuch
Use the following code:
function catch_that_image( $size = 'full' ) {
global $post;
if ( has_post_thumbnail($post->ID) ) {
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), $size);
if(empty($featured_image[0])){ //Defines a default image
$featured_image[0] = "http://www.mywebsite/wp-content/themes/Default_Image.jpg";
}
return $featured_image[0];
}
return false;
}

How to change HTML tags and attributes using PHP in Wordpress?

In my wordpress blog I'm trying to set up a code/a function to turn every image in a post into a thumbnail using the phpThumbnail library.
So far I have this function which generates an array of images in a post and creates a thumbnail out of the first image using the _thumbnail_id meta key.
function sThumbnail() {
$post_id = get_the_ID();
$post = get_post($post_id);
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[1][0];
var_dump($matches);
$first_img = substr($first_img, strlen(site_url()));
$post_thumbnail = get_post_meta( $post_id, '_thumbnail_id', true );
if (!wp_is_post_revision($post_id)) { // Verify that post is not a revision
if (empty($post_thumbnail)) { // Check if Thumbnail does NOT exist!
if(empty($first_img)){
update_post_meta( $post_id, '_thumbnail_id', 'http://static.guim.co.uk/sys-images/Guardian/About/General/2012/11/1/1351779022327/Nicki-Minaj---You-ve-neve-010.jpg' );
} else {
update_post_meta( $post_id, '_thumbnail_id', $first_img );
} // Get the first image of a post (if available)
}
}
}
With some modification I could use the function to get all of the images in a post and then do something with them.
The problem is that I have no idea how to alter the src attribute of the <img> tag or delete it and enter a new one.
Basically, all I need is that 1-2 lines that alter the src attribute or some general info about how to alter/play with tags and their attributes.
Some points:
- Just to clarify, by src, I mean <img src="..." />.
- The function above is NOT the function I'm going to use!
It's merely for reference as the first 6/7 lines of it are the way to get the content of the src attribute of an image tag.

Query to show images with recent posts in Wordpress sidebar/widget

To show recent items from a Wordpress category in a widget I'm using this code...
<ul>
<?php $recent = new WP_Query("cat=1231&showposts=5"); while($recent->have_posts()) :
$recent->the_post();?>
<li><a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
</ul>
...but how can I make this query also display the first image in each post, and is there any way to set a 'default' image in case there is no image?
Is there also a way to use thumbnails here, rather than loading the full size image and using HTML to resize?
This is probably what you are looking for, found here
function get_first_image() {
global $post, $posts;
$first_img = '';
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
$first_img = “/images/default.jpg”;
}
return $first_img;
}

Categories