Title: Conditional Widgets based on sidebar
Last modified: April 11, 2017

---

# Conditional Widgets based on sidebar

 *  Resolved [idealbrandon](https://wordpress.org/support/users/idealbrandon/)
 * (@idealbrandon)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/conditional-widgets-based-on-sidebar/)
 * I am trying to conditional display some images based on which sidebar a widget
   lives in. I have these images defined as custom fields.
 * I am wondering if there is a way for an individual registered widget to know 
   which sidebar it is in using built in WordPress functionality (as opposed to 
   having to navigate the DOM via JavaScript, for example).
 * Any ideas?

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [9 years, 2 months ago](https://wordpress.org/support/topic/conditional-widgets-based-on-sidebar/#post-9019722)
 * Look at the plugin “widget context”. It will display or hide widgets based on
   a variety of criteria.
 *  Thread Starter [idealbrandon](https://wordpress.org/support/users/idealbrandon/)
 * (@idealbrandon)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/conditional-widgets-based-on-sidebar/#post-9019828)
 * Thanks for sending me that direction … It doesn’t quite do what I need it to 
   do, as I am not trying to conditional hide the widget, per se. Rather, I’m trying
   to hide pieces OF the widget based on its context …
 * I am currently exploring `wp_get_sidebars_widgets()` as it contains all of the
   active sidebars with their respective widgets by ID, so I should be able to compare
   IDs and grab the widget area from there.
 * Thanks again
 *  Thread Starter [idealbrandon](https://wordpress.org/support/users/idealbrandon/)
 * (@idealbrandon)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/conditional-widgets-based-on-sidebar/#post-9019955)
 * So! Just as an update for anyone who may be interested in doing the same thing
   as me, this is relatively simply.
 * Basically, I needed to know the ID of a given widgets parent widget area. To 
   achieve this, inside the widget() function (in the WP_Widget class) I did the
   following:
 *     ```
       $this_widget_id = $args['widget_id'];
       $all_widgets = wp_get_sidebars_widgets();
       $this_widget_area_id;
   
       foreach ( $all_widgets as $widget_area => $widget_ids ) {
   
           foreach ( $widget_ids as $widget_order => $widget_id ) {
               if ( $widget_id == $this_widget_id )
                   $this_widget_area_id = $widget_area;
               }
   
       }
       ```
   
 * This allows me to conditionally target pieces of a widget based on the widgets
   location.
 * **EDIT** While this technically solves the problem, it uses the private WP function
   wp_get_sidebars_widgets() to do so. I’ll find a solution that _doesn’t_ use a
   private function instead.
    -  This reply was modified 9 years, 2 months ago by [idealbrandon](https://wordpress.org/support/users/idealbrandon/).

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Conditional Widgets based on sidebar’ is closed to new replies.

## Tags

 * [widgets](https://wordpress.org/support/topic-tag/widgets/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 2 participants
 * Last reply from: [idealbrandon](https://wordpress.org/support/users/idealbrandon/)
 * Last activity: [9 years, 2 months ago](https://wordpress.org/support/topic/conditional-widgets-based-on-sidebar/#post-9019955)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
