• Resolved svisie

    (@svisie)


    Some of my posts are using the featured images of WordPress, others are using custom fields (the data of the field looks like <img src="FILELOCATION" class="NAME" alt="TEXT" title="TEXT" width="520" height="250" />). This has to do with several reasons like old posts, new posts, importing etcetera.

    I have problems with combining those two. I want WordPress to show the data of the custom field if it exists, or else to show the featured image (or vice versa). Both codes works correctly if they are completely seperated from each other, but I keep getting errors when I try to combine them.

    Codes:

    <?php  echo '<a href="', get_permalink(), '">';
    if (has_post_thumbnail()) {
        the_post_thumbnail('NAME');
    }
    else {
        echo
            '<img src="',
            get_bloginfo('template_directory'), '/images/logo.png',
            '" width="520" height="250" alt="logo" />';
    }
    echo '</a>';  ?>

    and

    <?php  echo '<a href="', get_permalink(), '">';
    			 $key="NAME"; echo get_post_meta($post->ID, $key, true);
    			 echo '</a>';
    			   ?>

    Any tips or suggestions?

Viewing 2 replies - 1 through 2 (of 2 total)
  • I don’t have a good way to test this, but it should be close to what you want:

    <?php  echo '<a href="', get_permalink(), '">';
    $key = "NAME";
    if ( $custom_img = get_post_meta($post->ID, $key, true) ) {
       echo $custom_img;
    } elseif (has_post_thumbnail()) {
        the_post_thumbnail('NAME');
    } else {
        echo
            '<img src="',
            get_bloginfo('template_directory'), '/images/logo.png',
            '" width="520" height="250" alt="logo" />';
    }
    echo '</a>';  ?>
    Thread Starter svisie

    (@svisie)

    Sorry, it took some time to test it, but it worked 🙂
    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Combining custom fields and featured images’ is closed to new replies.