Thumbnail_html, featured image

Hello,

I am still setting up X - The Theme on my 11 year old Wordpress blog. There are some issues with older posts, that I cannot possibly manually change.

In older posts I do not have a featured picture, instead I have a custom field, thumbnail_html that has the html code for the picture

thumbnail_html
<img src="http://www.wolf-hund.info/wp-content/uploads/sites/3/2017/07/SONY-ILCE-6000-20160704-122848-DSC03669-web-400x225.jpg" class="attachment-thumbnail size-thumbnail" alt="" />

Is it possible, in case a featured image does not exist, to read this “meta key” and display the image instead of featured?

Hi There,

Please add the following code under functions.php file locates in your child theme:

function x_featured_image( $cropped = '' ) {

    $stack     = x_get_stack();
    $fullwidth = ( in_array( 'x-full-width-active', get_body_class() ) ) ? true : false;

    if ( has_post_thumbnail() ) {

      if ( $cropped == 'cropped' ) {
        if ( $fullwidth ) {
          $thumb = get_the_post_thumbnail( NULL, 'entry-cropped-fullwidth', NULL );
        } else {
          $thumb = get_the_post_thumbnail( NULL, 'entry-cropped', NULL );
        }
      } else {
        if ( $fullwidth ) {
          $thumb = get_the_post_thumbnail( NULL, 'entry-fullwidth', NULL );
        } else {
          $thumb = get_the_post_thumbnail( NULL, 'entry', NULL );
        }
      }

      switch ( is_singular() ) {
        case true:
          printf( '<div class="entry-thumb">%s</div>', $thumb );
          break;
        case false:
          printf( '<a href="%1$s" class="entry-thumb" title="%2$s">%3$s</a>',
            esc_url( get_permalink() ),
            esc_attr( sprintf( __( 'Permalink to: "%s"', '__x__' ), the_title_attribute( 'echo=0' ) ) ),
            $thumb
          );
          break;
      }

    } else {
    	echo '<img src="http://www.wolf-hund.info/wp-content/uploads/sites/3/2017/07/SONY-ILCE-6000-20160704-122848-DSC03669-web-400x225.jpg" class="attachment-thumbnail size-thumbnail" alt="" />';
    }

}

Thank You for your tipps. However the picture does not show up. I tried to change the echo command to read the contents of the post meta key “thumbnail_html”

// Featured Image
// =============================================================================

function x_featured_image( $cropped = '' ) {

    $stack     = x_get_stack();
    $fullwidth = ( in_array( 'x-full-width-active', get_body_class() ) ) ? true : false;

    if ( has_post_thumbnail() ) {

      if ( $cropped == 'cropped' ) {
        if ( $fullwidth ) {
          $thumb = get_the_post_thumbnail( NULL, 'entry-cropped-fullwidth', NULL );
        } else {
          $thumb = get_the_post_thumbnail( NULL, 'entry-cropped', NULL );
        }
      } else {
        if ( $fullwidth ) {
          $thumb = get_the_post_thumbnail( NULL, 'entry-fullwidth', NULL );
        } else {
          $thumb = get_the_post_thumbnail( NULL, 'entry', NULL );
        }
      }

      switch ( is_singular() ) {
        case true:
          printf( '<div class="entry-thumb">%s</div>', $thumb );
          break;
        case false:
          printf( '<a href="%1$s" class="entry-thumb" title="%2$s">%3$s</a>',
            esc_url( get_permalink() ),
            esc_attr( sprintf( __( 'Permalink to: "%s"', '__x__' ), the_title_attribute( 'echo=0' ) ) ),
            $thumb
          );
          break;
      }

    } else {

			$thumb = get_post_meta( get_the_ID(), 'thumbnail_html', true );

				switch ( is_singular() ) {
      	case true:
      		printf( '<div class="entry-thumb">%s</div>', $thumb );
      		break;
        case false:
					printf( '<a href="%1$s" class="entry-thumb" title="%2$s">%3$s</a>',
						esc_url( get_permalink() ),
						esc_attr( sprintf( __( 'Permalink to: "%s"', '__x__' ), the_title_attribute( 'echo=0' ) ) ),
						$thumb
					);
					break;
      }

    }
}

I also added the function from classes.php to the x-child/functions.php and added an “or” to the “has_post_thumbnail()” part to enable the “thumbnail_html” meta key.

if ( ! function_exists( 'x_post_class' ) ) :
  function x_post_class( $output ) {

    switch ( has_post_thumbnail() || metadata_exists( 'post', get_the_ID(), 'thumbnail_html' ) ) {
      case true :
        $output[] = 'has-post-thumbnail';
        break;
      case false :
        $output[] = 'no-post-thumbnail';
        break;
    }

    return $output;

  }
  add_filter( 'post_class', 'x_post_class' );

endif;

Now each post that has a “thumbnail_html” meta key will display a placeholder, but the code for the thumbnail (or the echo command above, or even "echo '123'; )

Any ideas? :slight_smile:

I am trying another solution and will get back to this topic in a bit.

Sure, do update us if you need any assistance.

Thanks!

Thank You for your help… I got a different solution working now :slight_smile:

Awesome! We are just glad that you have figured it out a way to correct the said issue.
Thanks for letting us know!