• stiwdio

    (@stiwdio)


    Wonder if someone to add to my code;

    I have a simple latest post in the sidebar, but have the an image, if saved in the custom field, for the post as the background of a div with rounded corners;

    It works well, but now I want to add a code to add a custom image if one isnt uploaded; Can someone help me out?

    Cheers

    <?php
    $recentPosts = new WP_Query();
    $recentPosts->query('showposts=1');
    while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <?php $post_back = get_post_meta($post->ID, 'homepage-thumbnail', $single = true);?>
    <div class="newsimage" style="background: url('<?php echo $post_back; ?>') no-repeat; background-size: 100%;"></div>
    <span class="sidetitle"><a href="<?php the_permalink() ?>" rel="bookmark"
        title="Link to <?php the_title(); ?>">
    <?php the_title(); ?></a> </span><br />
    <?php echo get_excerpt(150); ?>
    <?php endwhile;?>
    </div>
Viewing 6 replies - 1 through 6 (of 6 total)
  • <?php if ( get_post_meta($post->ID, 'homepage-thumbnail', true) ) {?>
    <?php $post_back = get_post_meta($post->ID, 'homepage-thumbnail', $single = true);?>
    <?php endif; ?>

    Then use <?php } else {?> to add code for your custom images.

    Thread Starter stiwdio

    (@stiwdio)

    Hi Milmor

    Thanks for this, I haven’t got a clue though could you show me exactly hwo I should write it?

    Thread Starter stiwdio

    (@stiwdio)

    Maybe I should add this is in the sidebar

    Please can someone help me

    Try with this:

    <?php
    if ( get_post_meta($post->ID, 'homepage-thumbnail', true) ) {
    $post_back = get_post_meta($post->ID, 'homepage-thumbnail', $single = true);
    echo '<div class="newsimage" style="background: url(' . $post_back . ') no-repeat; background-size: 100%;"></div>';
    } else {
    echo '<div class="newsimage" style="background: url(yourhttpimagepath) no-repeat; background-size: 100%;"></div>';
    }
    ?>

    You have to replace “yourhttpimagepath” with the alternative image

    prionkor

    (@prionkor)

    Thread Starter stiwdio

    (@stiwdio)

    Thank you Milmor
    That worked well;

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Adding Default Thumbnail’ is closed to new replies.