Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

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.

share|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
show 3 more comments
up vote 0 down vote accepted

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') {
                continue;
            }

            // 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()) {
                unset($sidebars_widgets[$widget_area][$pos]);
            }
        }
    }
    return $sidebars_widgets;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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