Techniqued- Technoloy, Review and Tutorials
  • Android
  • Freewares
  • Tutorial
  • Windows 11
  • macOS
No Result
View All Result
Techniqued- Technoloy, Review and Tutorials
  • Android
  • Freewares
  • Tutorial
  • Windows 11
  • macOS
No Result
View All Result
Techniqued- Technoloy, Review and Tutorials
No Result
View All Result

How to Get the First Image from a WordPress Post

Nirmal by Nirmal
November 12, 2011
in Wordpress
0
Share on FacebookShare on Twitter

Most of the themes available for WordPress nowadays uses the much popular TimThumb script which can resize images on the fly. The script actually needs an image from the post and then resizes them according to the size you have specified. But for fetching the image, most of the themes uses custom fields and specifying the custom field for getting an image from the post. When publishing post, you need to specify a custom field and then add the image URL as the value. But with a simple php function, you can easily fetch the first image from any post, here is how to get it.

Wordpress

Open the functions.php file in your WordPress theme currently being used. At the end of the functions, add the below script.

// Get URL of first image in a post
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];
// no image found display default image instead
if(empty($first_img))
{
$first_img = "/images/default.jpg";
}
return $first_img;
}

Now save the file and call the function <?php echo get_first_image() ?>  in your template file within the loop. So for each post, it gets the first image and this value can be passed to TimThumb script for resizing images on the fly.

This function makes it easy to use the resize script without having to use any custom fields or settings.

Thanks to WordPress forums for this tip.

Tags: Resize ImagesTimthumbTipsTutorialsWordpress
Previous Post

New Windows 7 Themes- Green World, Macro World of Rocks and Autumn Color

Next Post

Profile Manager for Firefox

Related Posts

Wordpress

How To Fix “The Link You Followed Has Expired” Error in WordPress

December 9, 2020
Wordpress

Install WordPress Locally on Windows with Local

February 24, 2020
Windows 10

How to Create a Windows 10 App for WordPress Blog

March 21, 2016
Wordpress

7 Ways to Install WordPress Locally on Windows or Mac

January 9, 2015
Wordpress

5 Ways To Reduce Spam Comments Spam On WordPress

January 8, 2015
Wordpress

10 Best WordPress Backup Services for your Blog

January 5, 2015

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.




Latest Articles

Tutorial

How to Enable Focus Mode in Windows 11

February 22, 2022
Reviews

Movavi Video Converter- a Versatile Video Converter with support for 180+ Formats

February 16, 2022
Windows 11

What is Reserved Storage in Windows 11

February 6, 2022
Tutorial

How to Reset Windows 11 without Losing Files

February 5, 2022
Techniqued- Technoloy, Review and Tutorials

© 2021 Techniqued - Technology, Tips, Tricks and Reviews

Navigate Site

  • About Us
  • Advertise with us
  • Contact Us
  • Copyright Policy
  • Disclaimer

Follow Us

No Result
View All Result
  • Android
  • Freewares
  • Tutorial
  • Windows 11
  • macOS

© 2021 Techniqued - Technology, Tips, Tricks and Reviews