Support » Fixing WordPress » 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 ?

Viewing 15 replies - 1 through 15 (of 19 total)
  • sounded like fun but I couldn’t do it. I was trying this, which appears to do the same or similar.

    DeepBevel
    i already tried your link , but not working
    in above code , it seems the
    $first_img=”/path/default.png”
    need only html string

    is there , some one who can guide?

    I put this in functions:

    // 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];
    return $first_img;
    }

    In loop.php, for search and archives, I replaced

    <?php the_post_thumbnail();?>

    with:

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

    now, if no thumbnail is assigned, it gets the first image for the post. otherwise, it shows a default image for that posts categogry.

    I realize this is a different method than what’s described in the link. That didn’t work for me either. But this did. Still not what you need?

    arg, false alarm. It’s not getting the first image, only default cat image if no thumbnail assigned. sorry!

    This first looks for the_post_thumbnail, then “main_image” which is a function I added to get the first post gallery image (didn’t work with embedded) then finally, it looks for the assigned default category image.

    <?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="http://www.foursticks.net/only-a-test/wp-content/themes/twentyten/category-images/<?php $category = get_the_category(); echo $category[0]->cat_name; ?>.jpg" /> 
    
    <?php } ?>

    this is the function:

    function main_image() {
    $files = get_children('post_parent='.get_the_ID().'&post_type=attachment
    &post_mime_type=image&order=desc');
      if($files) :
        $keys = array_reverse(array_keys($files));
        $j=0;
        $num = $keys[$j];
        $image=wp_get_attachment_image($num, 'large', true);
        $imagepieces = explode('"', $image);
        $imagepath = $imagepieces[1];
        $main=wp_get_attachment_url($num);
    		$template=get_template_directory();
    		$the_title=get_the_title();
        print "<img src='$main' alt='$the_title' class='frame' />";
      endif;

    no false alarm this time, thoughly tested with featured image, with gallery image, and without either to get the default category image. But I still have a problem of not knowing how to size the first gallery image..? it appears full size, maybe I can add a class or something.

    also, of course a directory must be created for the category images, with images named after categories. “Uncategorized.jpg”

    wow !
    nice its working fine
    only i ve to add “}” at the end of function after “endif;”

    thank u all “deepbevel”

    how to add timthumb.php
    may be we need to add timthum.php code 3 times ?
    1 for post thumbs
    2nd for main image
    3rd for default image
    ???

    actually, I still don’t have it quite right. On my site the default image appears with the main_image, I get both! Something happened where my default images were so big I couldn’t tell which post had the default thumb (it was late).
    The sizing is yet another isseue to resolve.

    i ve tried wordpress add image size code but it is not working with it

    now i m controling image size with the help of CSS
    but it is not a good idea

    hmmm. yes, the function already has a place where a size is defined, but it has no effect.

    what about the default cateory image? does it not appear when the main_image is used?

    I thought maybe this code doesn’t define the condition where the image is actually used, only if it exists:

    } else if (  (function_exists('main_image')) && (main_image())  ) {

    i tried has_main_image, but no go.

    the default category image is being resized easily with the help of timthumb.php scrip

    <img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=http://www.urdusite.com/wp-content/themes/zenko/cat-images/<?php $category = get_the_category(); echo $category[0]->slug; ?>.jpg&w=450&h=350&zc=1" alt="<?php the_title(); ?>" />

    but if we apply the code to resize post_thumb

    echo get_the_post_thumbnail($post->ID,'thumb-mediam');

    it is giving 2 out puts
    it will disply post_thumb as well as default category image , means the post will display 2 images
    while no effect of size on post_thumbnail

    same to main image

    —————————————
    i ve applied the code successfully on my site

    www.urdusite.com

    but i ve to re write whole the theme just to replace theme orignal image code
    +
    i ve to control the size of thumbs with the help of CSS and definately it is not a good idea as it is just compressing image thus a lot of bandwith is being used if you have many images at site

    at least good to know the conditional is right. So, I’d have to re do my theme’s image code to make it work right? If I wanted to do the same what would I change?

    Also, even if I don’t use the size definitions you described,I still get both main_image and category image.

    my post_thumbnails default to the post thumbnail size, long and cropped.

    i suppose my theme is using it’s default image code, even when not defined in the tags..? may account for why i still get both default category and main_image at once?

    As you can tell my coding is rather weak. Given what you said about re writing your theme, do you think it’s too much to pursue? I don’t even need it, just thought it would be good to know how to do.

    I thought it was great just to have the empty featured image fall back to a default category image. perhaps I’ll settle.

    i m not sure but may be
    your theme is showing both images due to

    1- one image is being displayed by your code

    2- 2nd image is being displayed due to the

    <?php the_content(' '); ?>

    and the above code will fetch image automatically if the theme function.php has built in image coding for the_content

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