Swap out the primary sidebar for page specific widgetarea


Hey @Thierry

I'm working on the theme I mentioned the other day and have a quick question about swapping out the primary sidebar widgetarea for a page specific one. Ie, load the frontpage widgets on the frontpage and the default primary sidebar widgets on the blog.

Got any ideas?

Chris


Hi Chris,

There are multiple approach depending on your needs. If it is just to have a different widget area on the home page, I would suggest to register another sidebar and display it instead of the default primary sidebar on the front page as follow:

add_action( 'widgets_init', 'example_widgets_init', 2 );
/**
 * Register example widget area.
 */
function example_widgets_init() {
 beans_register_widget_area( array(
    'name' => __( 'Sidebar Primary Alt', 'smartcom' ),
    'id'   => 'example_primary_alt',
  ) );
}

beans_modify_action_callback( 'beans_widget_area_sidebar_primary', 'example_widget_area_sidebar_primary' );
/**
 * Echo primary widget area alt on front page and default primary widget area elsewhere.
 */
function example_widget_area_sidebar_primary() {

  // Load the primary alt sidebar on the front page.
  if ( true === is_front_page() ) {
   echo beans_widget_area( 'example_primary_alt' ); // WPCS: XSS ok.

   return;
 }

 beans_widget_area_sidebar_primary();

}

If you needed that to be "controllable" in the backend layout options, then you would need hook into beans layout options to register an new layout option and a listener for it in your callback function.

Have fun,


Thanks dude, that's exactly what I was after 😀

Write a reply

Login or register to write a reply, it's free!