Display video in place of featured image, if video exists (1 post)

  1. cindango
    Posted 3 years ago #

    Hello, apologies in advance for my poor understanding of PHP.

    Here's what's going on: I've installed a plugin that allows you to attach a featured video to a post, which works by dropping the embed code into a field. Currently the homepage has a big featured image that is either pulled from the latest post, or the latest sticky post.

    What I want is: If there is a video attached to a post, I want it to appear where the featured image would be. It would also override the uploaded featured image. I'm assuming some sort of conditional statement is required, but I'm not sure how to pull it off.

    Here's the website: http://theamericanreader.com


    Here is my attempt:

    <?php $latest = new WP_Query( array( 'post__in' => get_option( 'sticky_posts' ),
    'ignore_sticky_posts' => 1, 'posts_per_page' => 1, 'post_type' => array( 'post', 'fiction', 'poetry', 'criticism', 'interviews', 'translations' ) ) );
    if( $latest->have_posts() ) :
    while( $latest->have_posts() ) : $latest->the_post();
    $posttype = get_post_type( $post->ID );
    $video = the_post_video ( $post->ID );
    if ( $video > 0 ) echo the_post_video();
    if (class_exists('MultiPostThumbnails')
    && MultiPostThumbnails::has_post_thumbnail($posttype, 'article-image')) :
    $custom = MultiPostThumbnails::get_post_thumbnail_id($posttype, 'article-image', $post->ID); $custom=wp_get_attachment_image_src($custom, $posttype.'-article-image-thumbnail');
    echo '<a href="'.get_permalink( $id ).'" style="display:block; background-image: url('.$custom[0].');
    background-repeat: no-repeat;
    background-size: cover;
    -moz-background-size: cover;
    background-position: center; width:100%; height:475px; margin-bottom:90px; margin-top:87px;">';
    echo '';
    endif; wp_reset_postdata(); ?>


    Any ideas?

Topic Closed

This topic has been closed to new replies.

About this Topic