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

Related posts

3 comments

  1. 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.

  2. You can put your code wherever you want to show the sidebar like on header or on left or right based on your theme file.

    if ( function_exists( 'dynamic_sidebar' ) && dynamic_sidebar( $selected_sidebar_replacement) ) { 
         /*code to display featured image here*/
         global $post;
    
         if ( has_post_thumbnail( $post->ID ) )
            echo get_the_post_thumbnail( $post->ID, 'your-image-size' );
         } else {
         /*else do this*/
         }
    } else {
        /*else do this*/
    }
    
  3. Your conditions to display the sidebar might be met, but there is no command to display the sidebar:

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

    You should have

    if (function_exists('dynamic_sidebar') && dynamic_sidebar($selected_sidebar_replacement)) { 
    *code to display featured image here*
    } else {
    *else do this*
    } ?>
    

Comments are closed.