3

Before deciding to ask this question, I googled and searched this forum, but found no answer to my question, even though it may seem like a duplicate.

Anyway, I made custom post type that uses its featured image. Now, I would like to set if no featured image exists, show post content and by that I mean show whatever it is in my post ( in my case it is embedded youtube video).

So far I added to functions.php the following:

function zm_get_backend_preview_thumb($post_ID) {
    $post_thumbnail_id = get_post_thumbnail_id($post_ID);
    if ($post_thumbnail_id) {
        $post_thumbnail_img = wp_get_attachment_image_src($post_thumbnail_id, 'thumbnail');
        return $post_thumbnail_img[0];
    }
}

function zm_preview_thumb_column_head($defaults) {
    $defaults['featured_image'] = 'Image';
    return $defaults;
}

add_filter('manage_posts_columns', 'zm_preview_thumb_column_head');

function zm_preview_thumb_column($column_name, $post_ID) {
    if ($column_name == 'featured_image') {
        $post_featured_image = zm_get_backend_preview_thumb($post_ID);
            if ($post_featured_image) {
                echo '<img src="' . $post_featured_image . '" />';
            }

    }
}

add_action('manage_posts_custom_column', 'zm_preview_thumb_column', 10, 2);
}

And in my page where I would like to show the video instead featured image, I have the following code:

<?php
// WP_Query arguments
$args = array (
    'post_type'              => array( 'zm_gallery' ),
);
// The Query
$query_gallery = new WP_Query( $args );

// The Loop
if ( $query_gallery->have_posts() ) {

    while ( $query_gallery->have_posts() ) {
        $query_gallery->the_post();

        echo '<ul>';
        echo '<li>';
        $name = get_post_meta($post->ID, 'ExternalUrl', true);

        if( $name ) { ?>
            <a href="<?php echo $name; ?>"target="_blank"><?php the_post_thumbnail(); ?></a>
           <?php
        } else {
            the_post_thumbnail();
        }   

        echo '</li>';
        echo '</ul>';
    }    
} else {
    if ( "" === $post->post_content ) {
        the_post_thumbnail();
    } else {
        the_content();
    }
}

// Restore original Post Data
wp_reset_postdata();
?>

Your help would be highly appreciated. Thank you all in advance.

2 Answers 2

4

the_post_thumbnail will echo out the thumbnail so maybe try

if ( has_post_thumbnail() ) {
  the_post_thumbnail();
} else {
  the_content();
}

Hope this helps

1
  • Thanx for the reply @David. Luckily, I managed to solve this myself. Thanx all the same, your answer is correct anyway. Cheers!
    – Nancy
    Oct 8, 2016 at 7:09
0

I managed to realize that I had two problems with my code on the page where I needed post content displayed:

  1. I put the following:

        if ( "" === $post->post_content )
        {
             the_post_thumbnail();
        }
        else
        {
            the_content();
        } 
    

    in the wrong place.

    I should have put this bellow the line:

    if( $name ) { ?>
            <a href="<?php echo $name; ?>"target="_blank"><?php the_post_thumbnail(); ?></a>
            <?php } else {
        //MY CODE SHOULD GO HERE
    } 
    
    1. The only code I should have put instead of the code under point 1 is:

      the_content();
      

That got me what I wanted. And problem solved. I hope someone finds this helpful in future. P.s. I tried formatting the answer, but something is wrong. I would gladly accept edit to make it look as it should.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.