• Hi,
    I am stuck with some conditional statement that checks wether a post has a thumbnail. If the post has a thumbnail it should print the thumbnail, if not it should print the excerpt.
    I am aware of the function has_post_thumbnail but since I am using the DP Thumbnail plugin this function is not working as it should.

    I have been messing around with code but can not get it to work. Any help is greatly appreciated.
    This is my code

    <?php
    $IDOutsideLoop = $post->ID;
    
    $args = array( 'numberposts' => 100, 'orderby'=> 'post_date');
    $postslist = get_posts( $args );
    foreach ($postslist as $post) :  setup_postdata($post); ?>
    <div class="post">
    		<div class="image" <?php if(is_single() && $IDOutsideLoop == $post->ID) print 'style="visibility:hidden";' ?>><?php dp_post_thumbnail(); ?></div>
    		<div class="hover">
    			<div class="titel"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
    		</div>
    </div>
    <?php endforeach; ?>

    Basically I would want something simple as:

    if (dp_post_thumbnail());{
    <div class="image" <?php if(is_single() && $IDOutsideLoop == $post->ID) print 'style="visibility:hidden";' ?>><?php dp_post_thumbnail(); ?></div>
    }else {
    <?php the_excerpt();?>
    }

    But this doesn’t seem to work. Any ideas?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter lola rennt

    (@laurenserge)

    Okay, I got this code now:

    <?php if ( function_exists( 'dp_post_thumbnail' ) ) {
    dp_post_thumbnail();
    } else {
    the_excerpt();
    }
    ?>

    And this code correctly checks wether or not the post has a thumbnail attached to it. But still it will no return the excerpt if the has no thumbnail. And that is what I would like to achieve. How can I do that?

    I do not know anything about this thumbnail, but your code does the following:
    – if the plugin is activated, then it calls the function
    – only if the plugin is de-activated, the_excerpt() function is called.
    I think you want to do this:

    <?php if ( function_exists( 'dp_post_thumbnail' ) ) {
    if(!empty(dp_post_thumbnail()){
     dp_post_thumbnail();
    } else {
    the_excerpt();
    }
    }
    ?>

    This only works if the dp_post_thumbnail() function returns a value. Please check that.

    <?php if ( has_post_thumbnail() ) { ?>

    u have to enable the featured image too (in functions)

    if ( function_exists( 'add_theme_support' ) ) {
      add_theme_support( 'post-thumbnails' );
    }

    to post thumbnail (show it)

    <?php the_post_thumbnail('thumbnail'); ?>

    I hope this helps

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Conditional statement check if post has thumbnail’ is closed to new replies.