• Resolved DarrenMooney

    (@darrenmooney)


    I’m not the best with php, and having some problems getting a conditional statement to work.

    I’m using Video Thumbnails to generate thumbnails for videos. I’m using its conditional statement to only show a thumbnail if it has a video thumbnail, rather than it displaying its default one. However, in the even that no thumbnail is there I want the post thumbnail to display.

    I’m using a version of post thumbnail code to only generate the url of the post thumbnail, and both are place as inline css background images. However I also want different background sizes depending on which thumbnail is displayed.

    Right now what I’m using is messy:

    <div class="post-image" style="background-image: url(
    <?php if (has_post_thumbnail( $post->ID ) ): ?>
    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
    <?php echo $image[0]; ?>
    <?php endif; ?>
    );
    <?php if( ( $video_thumbnail = get_video_thumbnail() ) != null ) { echo " background-image: url(" . $video_thumbnail . ");"; } ?>
    "><a href="<?php the_permalink() ?>"></a></div>

    My goal

    If video thumbnail:

    background-image: url('video.png'); background-size: 180%;

    If post thumbnail:

    background-image: url('post.png'); background-size: 100%;

    I’ve tried to put together the conditional statement to only generate the background css based on the thumbnail that is present, but as I said, I’m not that good with php.

    Could I get some help from someone? Thanks so much ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • <?php
    echo '<div class="post-image" style="background-image: url("';
    if( ( $video_thumbnail = get_video_thumbnail() ) != null ) {
    	echo $video_thumbnail . '"); background-size: 180%;';
    } elseif (has_post_thumbnail( $post->ID ) ) {
    	$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
    	echo $image[0] . '"); background-size: 100%;';
    } else { // assume that there isn't any post thumb
    	echo get_bloginfo('stylesheet_directory') . '/images/nothumb.png"); background-size: 100%;';
    }
    echo '><a href="' . get_permalink() . '"></a></div>';
    ?>
    Thread Starter DarrenMooney

    (@darrenmooney)

    Thank you so much for your help, vjpo.

    Tested and working perfect.

    I really appreciate this, you’re awesome.

    ??

    UPD
    lost get_bloginfo('stylesheet_directory') .
    Good luck )

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Conditional statement for post & video thumbnails.’ is closed to new replies.