WordPress.org

Ready to get started?Download WordPress

Forums

Featured Video Plus
If else statement to either show an image or a video (7 posts)

  1. kerelberel
    Member
    Posted 11 months ago #

    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: http://www.hallometmirel.nl/emd (youtube post)

  2. kerelberel
    Member
    Posted 11 months ago #

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

  3. kerelberel
    Member
    Posted 11 months ago #

    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.

  4. Alexander Hoereth
    Member
    Plugin Author

    Posted 11 months ago #

    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
        }
    }
  5. kerelberel
    Member
    Posted 11 months ago #

    It doesn't resize the video: http://www.hallometmirel.nl/emd/?p=53 The content text should be on the right side like this: http://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.

  6. Alexander Hoereth
    Member
    Plugin Author

    Posted 11 months ago #

    $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
        }
    }
  7. kerelberel
    Member
    Posted 11 months ago #

    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 :)

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.