{"id":266,"date":"2011-08-19T15:23:07","date_gmt":"2011-08-19T06:23:07","guid":{"rendered":"http:\/\/www.santoshkori.com\/blog\/wordpress\/how-to-get-the-first-image-from-the-post-and-display-it-in-wordpress\/"},"modified":"2012-08-23T17:31:31","modified_gmt":"2012-08-23T08:31:31","slug":"how-to-get-the-first-image-from-the-post-and-display-it-in-wordpress","status":"publish","type":"post","link":"https:\/\/www.santoshkori.com\/blog\/how-to-get-the-first-image-from-the-post-and-display-it-in-wordpress\/","title":{"rendered":"How to Get the first image from the post and display it in WordPress"},"content":{"rendered":"<div id=\"_mcePaste\">\n<div id=\"_mcePaste\">&lt;div&gt;<\/div>\n<div id=\"_mcePaste\"><\/div>\n<div id=\"_mcePaste\">Most WordPress users are using custom fields to display thumbs on their blog homepage. It is a good idea, but do you know that with a simple php function, you can grab the first image from the post, and display it. Just read on.<\/div>\n<div id=\"_mcePaste\"><\/div>\n<div id=\"_mcePaste\">First, paste this function on your &lt;em&gt;functions.php&lt;\/em&gt; file.<\/div>\n<div id=\"_mcePaste\"><\/div>\n<div id=\"_mcePaste\">\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">&lt;\/div&gt;\r\n&lt;div id=&quot;_mcePaste&quot;&gt;function catch_that_image() {&lt;\/div&gt;\r\n&lt;div id=&quot;_mcePaste&quot;&gt;global $post, $posts;&lt;\/div&gt;\r\n&lt;div id=&quot;_mcePaste&quot;&gt;$first_img = '';&lt;\/div&gt;\r\n&lt;div id=&quot;_mcePaste&quot;&gt;ob_start();&lt;\/div&gt;\r\n&lt;div id=&quot;_mcePaste&quot;&gt;ob_end_clean();&lt;\/div&gt;\r\n&lt;div id=&quot;_mcePaste&quot;&gt;$output = preg_match_all('\/&lt;img.+src=&#x5B;\\'&quot;](&#x5B;^\\'&quot;]+)&#x5B;\\'&quot;].*&gt;\/i', $post-&gt;post_content, $matches);&lt;\/div&gt;\r\n&lt;div id=&quot;_mcePaste&quot;&gt;$first_img = $matches &#x5B;1] &#x5B;0];&lt;\/div&gt;\r\n&lt;div id=&quot;_mcePaste&quot;&gt;&lt;\/div&gt;\r\n&lt;div id=&quot;_mcePaste&quot;&gt;if(empty($first_img)){ \/\/Defines a default image&lt;\/div&gt;\r\n&lt;div id=&quot;_mcePaste&quot;&gt;$first_img = &quot;\/images\/default.jpg&quot;;&lt;\/div&gt;\r\n&lt;div id=&quot;_mcePaste&quot;&gt;}&lt;\/div&gt;\r\n&lt;div id=&quot;_mcePaste&quot;&gt;return $first_img;&lt;\/div&gt;\r\n&lt;div id=&quot;_mcePaste&quot;&gt;}&lt;\/div&gt;\r\n&lt;div id=&quot;_mcePaste&quot;&gt;<\/pre>\n<\/div>\n<div id=\"_mcePaste\">Once done, you can simply call the function within the loop to display the first image from the post:<\/div>\n<div id=\"_mcePaste\">\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">&lt;?php echo catch_that_image() ?&gt;<\/pre>\n<\/div>\n<div id=\"_mcePaste\">If you enjoyed this article, please consider sharing it!<\/div>\n<div id=\"_mcePaste\">&lt;\/div&gt;<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&lt;div&gt; Most WordPress users are using custom fields to display thumbs on their blog homepage. It is a good idea, but do you know that with a simple php function, you can grab the first image from the post, and display it. Just read on. First, paste this function on your &lt;em&gt;functions.php&lt;\/em&gt; file. &lt;\/div&gt; &lt;div [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,5],"tags":[129,130,131,132,725,133],"class_list":["post-266","post","type-post","status-publish","format-standard","hentry","category-tips-n-tricks","category-wordpress","tag-get-first-image-from-the-post-in-wordpress","tag-get-the-first-image-from-the-post","tag-get-the-first-image-from-the-wordpress-post","tag-how-to-get-the-first-image-from-the-post-and-display-it-in-wordpress","tag-wordpress","tag-wordpress-show-first-image"],"_links":{"self":[{"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/posts\/266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/comments?post=266"}],"version-history":[{"count":2,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/posts\/266\/revisions"}],"predecessor-version":[{"id":702,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/posts\/266\/revisions\/702"}],"wp:attachment":[{"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/media?parent=266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/categories?post=266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.santoshkori.com\/blog\/wp-json\/wp\/v2\/tags?post=266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}