2

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 -->
2
  • What is it exactly what you want? A Featured Image in a Widget? Why do you post your sidebar code then and not the code of the Widget? Commented Jul 11, 2013 at 6:38
  • yes its a widgets
    – Niks Niks
    Commented Jul 11, 2013 at 7:03

3 Answers 3

2

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
  • in side the widget code where i put this code?
    – Niks Niks
    Commented Jul 11, 2013 at 7:22
  • See the Widget API, you have to paste this code in the public function widget. Commented Jul 11, 2013 at 7:49
1

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*/
}
0

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*
} ?>
2
  • the post is display but no thumbnail image is display
    – Niks Niks
    Commented Jul 11, 2013 at 5:02
  • code to display featured image here from where to i put this code
    – Niks Niks
    Commented Jul 11, 2013 at 5:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.