In the Genesis Facebook group, a user asked:
how do i automagically rotate widgets in both the primary and secondary sidebars, on screen refresh? where do i find the index names of sidebars?
Adding the following in child theme’s functions.php will shuffle and display widgets of Primary and Secondary sidebars in random order on page refresh in WordPress:
add_filter( 'sidebars_widgets', 'custom_randomize_widget_order' );
/**
* Randomize order of widgets in Primary and Secondary sidebars.
*
* @param array $sidebars_widgets Existing associative array of sidebars and their widgets.
* @return array Modified associative array of sidebars and their widgets.
*/
function custom_randomize_widget_order( $sidebars_widgets ) {
$sidebars = array( 'sidebar', 'sidebar-alt' ); // set the ids of your sidebars whose widgets you want to shuffle
foreach ( $sidebars as $sidebar ) {
if ( isset( $sidebars_widgets[ $sidebar ] ) && ! is_admin() ) {
shuffle( $sidebars_widgets[ $sidebar ] );
}
}
return $sidebars_widgets;
}
Source: https://www.wp-code.com/wordpress-snippets/how-to-randomize-the-order-of-widgets/
I’ve worked with this code for a while now and am puzzled as to how to unset one of the widgets, so that it remains static, while the others will be shuffled. i’ve tried array_shift and unset, but i am either getting errors ( my php is quite rusty) or it doesn’t work. can you explain for example, how the primary sidebar ( sidebar_1 i believe is its id), the first widget at the top for example, can be removed from the shuffle?