Grab first image from Post in specific Category
-
Hi,
Thank you for this wonderful software!
I’m using the following code to grab the first image from the latest post
<?php echo get_first_image() ?>
which in turn calls this code
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 no image displays show below image if(empty($first_img)){ //Defines a default image $first_img = "/images/default.jpg"; } return $first_img; }
I would like modify the code, so it grabs the first image from the latest post in a given Category.
I’ve tried for several hours but I’m not good enough with the coding, so I was hoping for some help from the forum. Thanks.
- The topic ‘Grab first image from Post in specific Category’ is closed to new replies.