Skip to content

Instantly share code, notes, and snippets.

@luizlopescom
Last active May 25, 2020 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luizlopescom/18e382dc1264fc80efc27b603cfa4f9f to your computer and use it in GitHub Desktop.
Save luizlopescom/18e382dc1264fc80efc27b603cfa4f9f to your computer and use it in GitHub Desktop.
/**
* Randomize widgets in a given sidebar (index) and only display a single widget
*
* @See http://wordpress.stackexchange.com/a/152408/26350
*
* Good for Ad banners (simple)
*
*/
! is_admin() && add_filter( 'sidebars_widgets', function( $sidebars_widgets ) {
// ------------------------
// Edit this to your needs:
$sidebar_index = 'top_header';
// ------------------------
if( isset( $sidebars_widgets[$sidebar_index] ) )
{
// Randomize the array:
shuffle( $sidebars_widgets[$sidebar_index] );
// Slice the array:
$sidebars_widgets[$sidebar_index] = array_slice( $sidebars_widgets[$sidebar_index], 0, 1 );
}
return $sidebars_widgets;
}, PHP_INT_MAX );
//SAME FUNCTION
! is_admin() && add_filter( 'sidebars_widgets', function( $sidebars_widgets2 ) {
// ------------------------
// Edit this to your needs:
$sidebar_index2 = 'footerfull';
// ------------------------
if( isset( $sidebars_widgets2[$sidebar_index2] ) )
{
// Randomize the array:
shuffle( $sidebars_widgets2[$sidebar_index2] );
// Slice the array:
$sidebars_widgets2[$sidebar_index2] = array_slice( $sidebars_widgets2[$sidebar_index2], 0, 1 );
}
return $sidebars_widgets2;
}, PHP_INT_MAX );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment