WordPress.org

Ready to get started?Download WordPress

Forums

catch the first image if empty then default category image (20 posts)

  1. lovesaif
    Member
    Posted 2 years ago #

    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 ?

  2. deepbevel
    Member
    Posted 2 years ago #

    sounded like fun but I couldn't do it. I was trying this, which appears to do the same or similar.

  3. lovesaif
    Member
    Posted 2 years ago #

    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?

  4. deepbevel
    Member
    Posted 2 years ago #

    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.

  5. deepbevel
    Member
    Posted 2 years ago #

    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?

  6. deepbevel
    Member
    Posted 2 years ago #

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

  7. deepbevel
    Member
    Posted 2 years ago #

    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"

  8. lovesaif
    Member
    Posted 2 years ago #

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

    thank u all "deepbevel"

  9. lovesaif
    Member
    Posted 2 years ago #

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

  10. deepbevel
    Member
    Posted 2 years ago #

    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.

  11. lovesaif
    Member
    Posted 2 years ago #

    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

  12. deepbevel
    Member
    Posted 2 years ago #

    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.

  13. lovesaif
    Member
    Posted 2 years ago #

    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

  14. deepbevel
    Member
    Posted 2 years ago #

    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?

  15. deepbevel
    Member
    Posted 2 years ago #

    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.

  16. lovesaif
    Member
    Posted 2 years ago #

    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

  17. lovesaif
    Member
    Posted 2 years ago #

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

    <img src="http://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

  18. deepbevel
    Member
    Posted 2 years ago #

    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.

  19. lovesaif
    Member
    Posted 2 years ago #

    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(); ?>
  20. deepbevel
    Member
    Posted 2 years ago #

    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="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 } ?>

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic