Access function argument in different function?
I have this function:
<?php myWidget() { ?>
<div id="<?php echo $args['widget_id']; ?>">
<p>Something</p>
</div>
<?php } ?>
(widget_id is Wordpress core function so I have no direct access to that, it just simply generates widgets name)
I wanted to 开发者_JS百科add my widget as a shortcode so needed to create another function:
function myWidget_shortcode( $atts ) {
extract( shortcode_atts( array(
/* attributes here */
), $atts ) );
ob_start();
the_widget(myWidget);
return ob_get_clean();
}
the_widget just simply calls widget called myWidget.
The point is, everything works good, but id=" " is always empty when I use the second code.
I know it's a Wordpress question, but I believe this has more to do with my PHP code.
Any ideas?
Judging from your last comment the problem is that $args is not set and since the ID is set from $args it will "".
You have to find out where $args is set and either make sure that part is run or set it your self.
$args in this case is not arguments to myWidget method but a global array.
Check if it exists at all
isset($args)
My guess is its a scope issue. $args
isnt inside the function. Try passing it to the function
精彩评论