• I have a template which shows the featured image in the following way (I editted the original code to suit my needs):

    <?php
    if ( is_singular() && is_main_query() ) {
        if ( of_get_option('blog_single_thumbnail' ) == '1' && has_post_thumbnail() ) { ?>
            <div id="post-thumbnail">
                <img src="<?php echo aq_resize( wp_get_attachment_url( get_post_thumbnail_id() ),  wpex_img('blog_post_crop') ) ?>" alt="<?php echo the_title(); ?>" />
            </div>
        <?php }
    }

    Now I want an if statement which shows the video if the plugin is used. I’m having trouble with the proper syntax. How do I write it properly?

    <?php
    if ( is_singular() && is_main_query() ) {
        if ( of_get_option('blog_single_thumbnail' ) == '1' && has_post_thumbnail() ) { ?>
            <div id="post-thumbnail">
                <img src="<?php echo aq_resize( wp_get_attachment_url( get_post_thumbnail_id() ),  wpex_img('blog_post_crop') ) ?>" alt="<?php echo the_title(); ?>" />
            </div>
        <?php }
        elseif (has_post_video( $post_id ));{ ?>
            <div id="post-thumbnail">
                <?php echo get_the_post_video( $post_id, $size ); ?>
            </div>
        <?php }
    }

    Right now it shows the video and image together: https://www.hallometmirel.nl/emd (youtube post)

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter kerelberel

    (@kerelberel)

    I tried putting the entire if statement within the div but then it shows nothing.

    Thread Starter kerelberel

    (@kerelberel)

    Okay, a teacher explained to me that get_the_post_video comes out of nowhere, I asked what he meant and I think he meant it isn’t declared anywhere. I’m not so sure either because I don’t know much about PHP, and he doesn’t know much about WordPress.

    Still, the code I have in my first post shows both the image and the video. It reads both codes so I get_the_post_video does kind of work? Someone else adviced me to put the if statement inside the div.

    I’m not sure what to do, nor do I have a proper understanding of this.

    Plugin Author Alex

    (@ahoereth)

    Try this:

    <?php
    if ( is_singular() && is_main_query() ) {
        if ( has_post_video() ) { ?>
        <div id="post-thumbnail">
        <?php echo the_post_video(); ?>
        </div>
    <?php } elseif ( of_get_option('blog_single_thumbnail' ) == '1' && has_post_thumbnail() ) { ?>
        <div id="post-thumbnail">
            <img src="<?php echo aq_resize( wp_get_attachment_url( get_post_thumbnail_id() ),  wpex_img('blog_post_crop') ) ?>" alt="<?php echo the_title(); ?>" />
        </div>
    <?php
        }
    }

    Thread Starter kerelberel

    (@kerelberel)

    It doesn’t resize the video: https://www.hallometmirel.nl/emd/?p=53 The content text should be on the right side like this: https://www.hallometmirel.nl/emd/?p=36

    I tried adding $size to the echo of the_post_video but that didn’t work either. On another forum someone says the variable $post_id and have to be added first before they can be used. But $post_id does work right? otherwise it wouldn’t have found the video.

    Plugin Author Alex

    (@ahoereth)

    $size needs to be an array:

    <?php
    if ( is_singular() && is_main_query() ) {
        if ( has_post_video() ) { ?>
        <div id="post-thumbnail">
        <?php the_post_video( array( 439, 300 ) ); ?>
        </div>
    <?php } elseif ( of_get_option('blog_single_thumbnail' ) == '1' && has_post_thumbnail() ) { ?>
        <div id="post-thumbnail">
            <img src="<?php echo aq_resize( wp_get_attachment_url( get_post_thumbnail_id() ),  wpex_img('blog_post_crop') ) ?>" alt="<?php echo the_title(); ?>" />
        </div>
    <?php
        }
    }

    Thread Starter kerelberel

    (@kerelberel)

    What I eventually noticed is the YouTube plugin has its own div. I removed the 2nd mention of post-thumbnail and assigned some styling to the plugin div.

    There is a difference. With your code the video height is bigger and I get black bars. But I guess that’s more a CSS problem. Thanks for the effort ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘If else statement to either show an image or a video’ is closed to new replies.