WordPress.org

Ready to get started?Download WordPress

Forums

conditional image thumbnail on main blog page (8 posts)

  1. jonnyburch
    Member
    Posted 5 years ago #

    Hi, I am having a real issue with a website - I have managed to place thumbnails of post images on the front page of my website but some of the posts will not have images attached and at the moment that just leaves them with a blank space - I am very new to php and cannot code the appropriate if/else bit that excludes the thumbnail box when there isnt an image available.

    This is what i have so far:

    <?php $image = get_post_meta($post->ID, 'thumbnail', true); ?>
    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php echo $image; ?>" alt="" /></a>

    I have tried various things but always end up bodging them.
    some help would be most appreciated.

    Jonny

  2. SimonJ
    Member
    Posted 5 years ago #

    Hi Jonny,

    Replace your code with this one, it should work :

    <?php
    $image = get_post_meta($post->ID, 'thumbnail', true);
    $link = get_permalink($post->ID);
    if ($image == 0) {
    echo '';
    }
    else {
    echo  '<a href="'.$link.'" title="'.$post->post_title.'"><img src="'.$image.'" alt="" /></a>';
    }
    ?>

    This code looks if your $image variable is empty (if ($image == 0))

    If so, it echoes nothing.

    If there is something, it echoes the img tag enclosed in a link to the post.

    I'm far than being a php guru, so maybe there is a more fancy way to write it, but as I said, it should work... :-)

    S.

  3. SimonJ
    Member
    Posted 5 years ago #

    By the way, I wrote a simple function to display the thumbnail of the first image attached to a post.

    As I can see, you use the custom field to display your thumbnail. But you don't need to do so. You can automatically display a thumbnail if you uploaded an image via the write post interface.

    function the_post_thumbnail() {
    global $wpdb;
    global $post;
    $thumb = $wpdb->get_var("SELECT ID FROM $wpdb->posts where post_parent= $post->ID and post_type = 'attachment'");
    $thumbnail = wp_get_attachment_thumb_url($thumb);
    if ($thumb == 0) {
    echo '';
    }
    else {
    echo '<img src="'.$thumbnail.'" alt="" />';
    }
    }

    With this function in your functions.php (in your theme directory), you just have to use :

    <?php the_post_thumbnail();?>

    Anywhere in your loop to display the thumbnail... :-)

    S.

  4. jonnyburch
    Member
    Posted 5 years ago #

    Thanks simon -

    Unfortunately I tried putting your code into my functions.php and got this error whenever i tried to do anything else:

    Warning: Cannot modify header information - headers already sent by (output started at /home/MY SITE NAME/wp-content/themes/geex3m/functions.php:433) in /home/MY SITE NAME/wp-admin/theme-editor.php on line 62

    also the first code looks like it should work really well but doesn't
    Im sure it's something im doing wrong... that warning message has got me worried though. I have now got rid of the code but it's still doing it.

  5. SimonJ
    Member
    Posted 5 years ago #

    Hola Jonny

    This error message means one thing : you likely have a space, a break line or a blank character in : functions.php at the line 433.

    It's a usual error read here.

    http://codex.wordpress.org/Answers-Trouble_Shooting#Headers_already_sent

    To avoid this kind of error, always edit your file with a good text editor, like notepad++

    You can read the solutions in the codex page I linked above.

    To repair it fast, just upload a fresh copy of functions.php from the original theme folder you downloaded.

    ---

    For the first code I gave you, I didn't test it with a custom field, I just putted your original line as the first varable. But I'm pretty sure the rest of the code should work...

    But for the functions I gave you in my second message, I'm 100% sure it should work... I use it in almost all my sites and have it in my test installation with the last WP version. It works pretty well without the "header already sent" error. This error is not relative to the function itself.

    Just repair your functions.php and add the function with a good text editor, it should work.

    S.

  6. SimonJ
    Member
    Posted 5 years ago #

    Ok...

    And here is the solution for the code using the custom field :

    <?php
    $image = get_post_meta($post->ID, 'thumbnail', true);
    $link = get_permalink($post->ID);
    if ($image == '') {
    echo '';
    }
    else {
    echo  '<a href="'.$link.'" title="'.$post->post_title.'"><img src="'.$image.'" alt="" /></a>';
    }
    ?>

    I made a mistake with the if contidition :

    My mistake :
    if ($image == 0)

    It should be :
    if ($image == '')

    S.

  7. Indojepang
    Member
    Posted 5 years ago #

  8. focused313
    Member
    Posted 5 years ago #

    thanks Simon J, that worked much better than what I had on one of my sites. I think the script I had was also too heavy. Now I just have to make that thumbnail link to the post which shouldn't be too hard.

Topic Closed

This topic has been closed to new replies.

About this Topic