How to change the CSS style for an instance of a multi instance widget dynamically depending on the result of an SQL query? When the query returns nothing at all, I would like that the instance of the widget is not displayed. But if the query returns a result, I would that this result is displayed normally in the instance.

I much sought, in vain. Would someone have an idea?

  • Maybe you can use some of this logic Oct 30, 2015 at 12:59
  • Does query happen inside the widget code? Outside?
    – Rarst
    Oct 30, 2015 at 13:39
  • @ Pieter Thanks, I'll look at it closely. @Rarst The SQL query is done in the widget itself.
    – luciole135
    Oct 30, 2015 at 14:47

1 Answer 1


The description of your problem is very minimal but the obvious solution is simply to output widget data only when the query is not empty:

class cond_widget extends WP_Widget {

  public function __construct() {
    // widget actual processes  
        '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'];

     register_widget( 'cond_widget' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.