• Hi,

    I currently feature five thumbnails at the top of my website, but each time I have to add a new image manually to each post so that the web design doesn’t break.

    The current code in my index.php file is:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args= array(
    	'posts_per_page' => 5,
    	'paged' => $paged
    );
    query_posts($args);
    ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <?php $thumbnail = get_post_meta($post->ID, 'post_thumbnail', true); ?>     <li>         <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">         <img src="<?php echo $thumbnail; ?>" alt="<?php the_title(); ?>" />         <span><?php the_title(); ?></span></a>     </li> <?php endwhile; else: ?><p><?php _e('Sorry, no posts matched your criteria.'); ?></p><?php endif; ?></ul>

    I’ve downloaded various plugins and read lots of blog posts but don’t know exactly how to go about editing the ‘else’ part of the code to determine the URL of a generic thumbnail image.

    Any tips? I am not that used to PHP code so any advice would be grealy appreciated! My main for doing this is that I will then be able to instantly add posts from my iPhone without needing to go online to determine the post_thumbnail custom field.

    For reference, my website is http://www.jeniwren.com

Viewing 7 replies - 1 through 7 (of 7 total)
  • what about fetching the first five posts with thumbnail?
    http://wordpress.org/support/topic/display-the-post-only-if-thumbnail-is-set-up?replies=17

    just add another line to the $args:

    $args = array(
    'posts_per_page' => 5,
    'paged' => $paged,
    'meta_key' => '_thumbnail_id'
    );

    —-
    or if you insist, a default image:

    a:
    somewhere before the loop, add:

    <?php $default_img = get_bloginfo('stylesheet_directory') . '/images/default_thumb.jpg'; ?>

    (assuming that the default image is called default_thumb.jpg and is located in the /images folder of the theme; change to any absolute image path, if needed.)

    b:
    change here:
    <img src="<?php echo ($thumbnail) ? $thumbnail : $default_img; ?>" alt="<?php the_title(); ?>" />

    (untested)

    Thread Starter jeni_8686

    (@jeni_8686)

    Thanks for your quick reply. I added the “‘meta_key’ => ‘_thumbnail_id'” as you suggested but then my website wouldn’t load unfortunately.

    I prefer your suggestion of only surfacing the most recent 5 posts with an image, but again I can’t make sense of the index.php code to edit this successfully myself!

    make sure to have a comma at the end of the paged line:

    .....
    'paged' => $paged,
    'meta_key' => '_thumbnail_id'
    Thread Starter jeni_8686

    (@jeni_8686)

    Thanks, I managed to work it out and it’s now fixed!

    Thread Starter jeni_8686

    (@jeni_8686)

    Oh no, i now accidentally deleted the first few lines of code so have broen my website and can’t easily access the previous version of my index.php code….!

    Thread Starter jeni_8686

    (@jeni_8686)

    One other point – when I did get the code working (although it was only for a few short minutes) it prevented the post (without a post_thumbnail value) from appearing at all, not just from the row of 5 thumbnails, but also from the main content section of the page?

    Thread Starter jeni_8686

    (@jeni_8686)

    Hi,

    Can anyone help me with this?

    I now know how to only feature the top five posts with thumbnails to the top of the page, but how do I also stop it preventing posts without thumbnails from being published?

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

The topic ‘Editing code to get a default post thumbnail image’ is closed to new replies.