catch the first image if empty then default category image
-
i m looking for enhancement in the following code
// Get URL of first image in a post 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]; // no image found display default image instead if(empty($first_img)){ $first_img = "/path/default.png"; } return $first_img; }
what i m looking is
1- if no image found “empty” then , it should display the default image from category like , i ve applied 1 code , but the value
$first_image =
is not accepting php code$first_img ="<?php bloginfo('template_directory'); ?>/cat-images/<?php $category = get_the_category(); echo $category[0]->slug; ?>.jpg";
may u guide me how to fix it ?
- The topic ‘catch the first image if empty then default category image’ is closed to new replies.