对问题的描述非常简单,但显而易见的解决方案是仅在查询不为空时输出小部件数据:
class cond_widget extends WP_Widget {
public function __construct() {
// widget actual processes
parent::__construct(
\'test\', // Base ID
\'test\', // Name
array( \'description\' => __( \'test\', \'foo\'))
);
}
public function widget( $args, $instance ) {
$qry = $wpdb->get_var(\'whatever\');
if (!empty($qry)) {
echo $args[\'before_widget\'];
if (!empty($instance[\'title\'])) {
$title = $instance[\'title\'];
echo $args[\'before_title\'].$title.$args[\'after_title\'];
}
echo \'<ul class="recent_pages">\';
echo \'</ul>\';
echo $args[\'after_widget\'];
}
}
}
add_action(
\'widgets_init\',
function(){
register_widget( \'cond_widget\' );
}
);