您可以尝试以下操作:
/**
* Only allow shortcodes for a widget with a given title.
*
* @see http://wordpress.stackexchange.com/a/160246/26350
*/
function wpse_widget_shortcode( $text, $instance )
{
// Only allow shortcodes for the widget with the following title:
$title = \'Some Title\';
remove_filter( current_filter(), __FUNCTION__ );
if( isset( $instance[\'title\'] )
&& mb_strtolower( $title ) === $instance[\'title\']
)
$text = do_shortcode( $text );
return $text;
}
add_filter( \'widget_text\', \'wpse_widget_shortcode\', 99, 2 );
在这里,我们通过标题来定位给定的小部件。