OK, I’ve had a problem with echoes in my last shortcodem, but everything works fine now.
But I have another one:
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? :>
Yes, look at the
widget()
method in yourMyWidget
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 itswidget()
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.