Show featured image in sidebar

I’m adding a widget on my sidebar but the Featured Image isn’t shown.

this is the code of my side bar:

<?php
/**
 * The Sidebar containing the main widget areas.
 *
 * @package PowerMag
 * @since PowerMag 1.0
 */
?>
    <div id="sidebar" <?php if ( of_get_option('pm_sidebar_position') == 'sidebar-content' ) { echo 'class="span4"'; } ?> >




        <div id="secondary" role="complementary">

        <?php do_action( 'before_sidebar' ); ?>         

        <?php 

                //Select wich sidebar will be display
                $selected_sidebar_replacement="sidebar-1"; //Default Sidebar

                //If is page or single.
                if(is_singular()){

                    global $wp_query;
                    $post = $wp_query->get_queried_object();
                    $selected_sidebar_replacement = get_post_meta($post->ID, 'sbg_selected_sidebar_replacement', true);

                    //If default selected
                    if($selected_sidebar_replacement == '0' || $selected_sidebar_replacement == ''){
                        $selected_sidebar_replacement="sidebar-1";
                    }

                    // Reset the global $the_post as this query will have stomped on it
                    wp_reset_query();

                }       

                if (function_exists('dynamic_sidebar') && dynamic_sidebar($selected_sidebar_replacement)) : else : ?>

            <p>You selected an empty sidebar, try populating it with some awesome widgets!</p>

            <?php endif; // end sidebar widget area ?>
        </div><!-- #secondary -->
    </div><!-- #sidebar -->

3 Answers
3

You can get the Post Thumbnail by get_the_post_thumbnail( ).
Inside your widget add the following code:

global $post;

if ( has_post_thumbnail( $post->ID ) )
  echo get_the_post_thumbnail( $post->ID, 'your-image-size' );

If there is a Featured Image, it will be shown.

Leave a Comment