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:
Since you’re not printing
$before_widget
and$after_widget
, there won’t be any output for this widget unless the conditions are met.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
: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: