Shortcode displays always first. Once again

OK, I’ve had a problem with echoes in my last shortcodem, but everything works fine now.

But I have another one:

Read More
function myWidget_shortcode( $atts ) {
        extract( shortcode_atts( array(
        'title' => 'My Widget',
        'value' => '5',
        ), $atts ) );

        return the_widget(myWidget,'title='.$title.'&value='.$value);
}

add_shortcode('myWidget', 'myWidget_shortcode'); 

Can you tell me wy this shortcode always displays first on pages? There’s no echo etc., all the data is being returned…

[found the answer – edit]

This resolves the problem:

ob_start();    
the_widget(popularPosts,'title='.$title.'&number='.$number);
return ob_get_clean();

Anyways I don’t understand why it’s always first in this case. Because the_widget is a function itself and echoes something? :>

Related posts

Leave a Reply

1 comment

  1. Yes, look at the widget() method in your MyWidget class. Does it echo? Most likely it does, because that’s how widgets are normally written. In fact, I’d be surprised to see a widget that didn’t echo output in its widget() method.

    And when you call the_widget(), it fetches an instance of the widget you ask for, and calls $widget_obj->widget($args, $instance);. So it echos, and doesn’t return anything.