• Resolved Ajay Tiwari

    (@mmajay)


    Hi,
    I am trying to get post thumbnail out of loop with a default thumbnail image for those posts which don’t have featured image, now issue is either i receive default thumbnail image for all posts OR thumbnail for those posts which have featured image & no thumbnail for posts which don’t have featured image.

    Is there any way by which i can get conditional thumbnail working. My code is:

    <img class="img-circle img-thumbnail" src="<?php if ( function_exists('wp_get_thumb_url')) { echo wp_get_thumb_url($value->ID);} else { echo get_template_directory_uri().'/assets/images/default-user.jpg'; } ?>" alt="<?php echo $value->post_title; ?>">

    And

    function wp_get_thumb_url($post_ID){
        return wp_get_attachment_url( get_post_thumbnail_id( $post_ID ) );
    }

    Thanks in advance for any help OR suggestions.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Try adding this snippet in the loop:

    $thumb = '';
    $thumbnail = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    if(!$thumbnail) {
    	$thumb = get_template_directory_uri().'/assets/images/default-user.jpg';
    } else {
    	$thumb = $thumbnail;
    }

    And see if $thumb can do it out of the loop..

    Thread Starter Ajay Tiwari

    (@mmajay)

    still no result.. Well, let me tell you it is for custom post type.

    i made few chnages in my code by which i am getting the featured image as the post thumbnail, here is the code:

    <img class="img-thumbnail" src="<?php echo wp_get_thumb_url($value->ID);?> />"

    And

    `function wp_get_thumb_url($post_ID){
    return wp_get_attachment_url( get_post_thumbnail_id( $post_ID ) );
    }’

    But when i am trying to add conditional thumbnail image either it shows featured image if post have it & no thumbnail for post which don’t have featured image OR it is showing the default thumbnail image for all posts even if i have the featured image which i want to show as thumbnail.

    Inside of the loop, see if this :

    wp_get_attachment_url( get_post_thumbnail_id($post->ID) )

    gets the post thumbnail. also try to call global $post. once it succeeds we will try another way to use this out of the loop.

    Otherwise, be more informative about this issue.

    Thanks Ajay!

    Thread Starter Ajay Tiwari

    (@mmajay)

    Hey Samuel,
    thanks for trying to help me buddy,
    well, here is the complete code i am using to get the thumbnail which is working fine and showing the thumbnail for posts which have featured image.

    $args = array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘myteam’
    );

    $data = get_posts( $args );
    if(count($data)>0){ ?>
    <div class=”team row”>
    <?php foreach ($data as $key => $post) { ?>
    <div class=”col-md-3 col-sm-4 col-xs-6″>
    <div class=”center team-member”>
    <p><img class=”img-thumbnail” src=”<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) );?>” /></p>

    in above code as you can see i have used what you suggested above but out ot loop which is working fine.

    Now what i want is if there is no thumbnail i want to show the default user image but unable to get it done..

    Now what i want is if there is no thumbnail i want to show the default user image but unable to get it done..

    Does the thumbnail now work inside the loop?

    I would use this to call a default image if thumbnail isn’t set:

    $args = array(
    'posts_per_page' => -1,
    'post_type' => 'myteam'
    );
    
    $data = get_posts( $args );
    if(count($data)>0){ ?>
    <div class="team row">
    <?php foreach ($data as $key => $post) {
    $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    $no_thumb = get_template_directory_uri().'/assets/images/default-user.jpg';
    ?>
    <div class="col-md-3 col-sm-4 col-xs-6">
    <div class="center team-member">
    <p><img class="img-thumbnail" src="<?php if($thumb != '') { echo $thumb } else { echo $no_thumb; } ?>" /></p>
    Thread Starter Ajay Tiwari

    (@mmajay)

    You are awesome Samuel.. that worked like a charm.. Thank you very much buddy. Not sure, how I can ever repay you what I owe you.

    I would love to connect with you. Not to disturb you with problems but possibly we can work together on mutually beneficial thing.

    Awesome! glad to hear you are fixed Ajay!

    There’s nothing you should repay me for, buddy! wishing you good luck with the rest of your project.

    Yes, I am available on Facebook, even though I hate it => fb.me/univ.soldier

    Hope to see you there!

    Have a nice day, and don’t forget to mark this topic as resolved if you feel that’s it.

    Thread Starter Ajay Tiwari

    (@mmajay)

    Just sent you add request on facebook. Have a great day buddy..

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Conditional thumbnail display not working’ is closed to new replies.