I've made a custom post type, with a few accompanying widgets, and I was wondering if it is possible to show (some of) these widgets only if you're on a specific page.

Currently I've got

function widget($args, $instance)
    global $post;
    $links = unserialize(get_post_meta( $post->ID, 'links', true ));

    if(!is_singular( 'press_articles' ) || $links == false) {
        unregister_widget( 'Point72_Press_Article_Links_Widget' );
        return false;

    // widget stuff

But, that doesn't seem to completely remove it, and with that I mean, it removes the widget in the right places, but the positions for the widgets stay even if it's not empty.

So, is there anyway to either remove the empty positions, or not show it at all outside a custom post type?


Instead of nuking the entire widget, you should put your conditional statements within the widget itself:

public function widget( $args, $instance ) {
     if ( is_singular( array( 'post_type_a', 'post_type_b' ) ) ) {
          // display your widget here

Since you're not printing $before_widget and $after_widget, there won't be any output for this widget unless the conditions are met.

| improve this answer | |
  • My bad, my current code is already with in the widget function, but I still get an empty position of the widgets area. – Hosh Sadiq Feb 22 '12 at 16:01
  • I hope you don't mind the edit. @Hosh Sadiq You've to add the post type names as array. – kaiser Feb 22 '12 at 16:01
  • 1
    @kaiser Not at all. :) Always open for improvement. – Brian Fegter Feb 22 '12 at 16:05
  • Btw: You should've named yourself Brain Fegter ;) – kaiser Feb 22 '12 at 16:06
  • @kaiser haha - I oft get misspelled to my delight. – Brian Fegter Feb 22 '12 at 16:14

Okay, I got it finally! Wouldn't have been able to without looking at the source of Widget Logic

This is how it is: First register a function to filter on sidebar_widgets:

add_filter( 'sidebars_widgets', 'my_filter_widgets', 10);

Then loop through all the widgets, skip over the widgets you don't care about, and if your condition is not met for the widgets you do care about, remove them, like so:

function my_filter_widgets($sidebars_widgets)
    foreach($sidebars_widgets as $widget_area => $widget_list)
        foreach($widget_list as $pos => $widget_id)
            // We're only after widgets named my_custom_widget, this can vary and you will need to find out, try doing a var_dump() on  $sidebars_widgets
            if(substr($widget_id, 0, 17) != 'my_custom_widget') {

            // Those that have not been skipped over, check your condition, and if it doesn't meet the condition, remove it from the $sidebars_widgets array
            if ('press_articles' != get_post_type() || !is_single()) {
    return $sidebars_widgets;
| improve this answer | |

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.