• 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 ?

Viewing 4 replies - 16 through 19 (of 19 total)
  • Thread Starter lovesaif

    (@lovesaif)

    some of my posts are being fetched and published automatically by RSS feeds , that posts have images like

    <img src="https://www.urdutimes.com/sites/default/files/National-Assembly-of-Pakistan.jpg?1341856759">

    look the text after “.jpg” like above is ?1341856759 , all these images are not being scaned/fetched by your code and these images are from external web site

    may u add some filter to scan these images also

    thanks, I think I see what you mean. I’m using twentyten, it’s just a test site. I’ll call it an on going project.

    Thread Starter lovesaif

    (@lovesaif)

    may u code the following idea ?

    ///////// function of any image /////////////////
    function get any image
    
    if post thumb 
    
    else
    
    if first image of post
    
    else
    if attachemnt of post
    
    else
    if any image scr external url in the post
    
    else
    
    image-src=<?php bloginfo('template_directory');?>/cat-images/<?php $category = get_the_category(); echo $category[0]->slug;?>.jpg";
    
    ////////////////  usage///////////////
    
    <?php any_image(); ?>

    I haven’t even figured out how to get this much to work yet.

    <?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) {
      echo get_the_post_thumbnail($post->ID);
    
    } else if (  (function_exists('main_image')) && (main_image())  ) {
    
    echo main_image();
    } else { ?>
    
    <img src="https://www.foursticks.net/only-a-test/wp-content/themes/twentyten/category-images/<?php $category = get_the_category(); echo $category[0]->cat_name; ?>.jpg" /> 
    
    <?php } ?>

    still getting default cat image and first post image at the same time. It’s in a div called “entry-summary” in my main loop. I’m showing it with the_excerpt. So I don’t know that any image featching is going on automatically which may cause the problem.

Viewing 4 replies - 16 through 19 (of 19 total)
  • The topic ‘catch the first image if empty then default category image’ is closed to new replies.