public
Last active

Hide WordPress' Categories widget on certain pages

  • Download Gist
hide_cats_widget.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<?php
/*
Plugin Name: Hide Categories Listing
Plugin URI: http://tarskitheme.com/
Description: Hide the Categories widget on certain pages.
Author: Benedict Eastaugh
Version: 1.0
Author URI: http://extralogical.net/
*/
 
function maybe_hide_categories_widgets($sidebars) {
if (!(is_page() || is_single())) return $sidebars;
// Add page IDs to this list, e.g.
// $hide_categories_on = array(1, 4, 9);
$hide_categories_on = array();
if (in_array(get_the_ID(), $hide_categories_on)
|| get_post_meta(get_the_ID(), 'hide_categories', true))
foreach($sidebars as $id => $sidebar)
foreach($sidebar as $i => $widget)
if (strpos($widget, "categories-") === 0)
unset($sidebars[$id][$i]);
return $sidebars;
}
 
add_filter('sidebars_widgets', 'maybe_hide_categories_widgets');
 
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.