Add sidebar to WooCommerce search results page

I’m running X + WooCommerce + WooCommerce Product Search.

I have a sidebar set up to show various widgets. This shows on all category and sub category pages.

I’d like to add this sidebar to the search results page.

I’ve tried customising search.php in my child theme, and content-search.php and wp-search.php in x-child>framework>views>icon, to no avail.

Any help would be greatly appreciated.

Hello There,

Thanks for writing in! Does your sidebar already have a sidebar and displays the main sidebar widget? If that is the case, please add the following code in your child theme’s functions.php file;

// Assign sidebar to search page 
// =============================================================================
add_filter( 'ups_sidebar', 'custom_search_sidebar' );
function custom_search_sidebar($sidebar){
	if( is_search() ){
		return 'ups-sidebar-sidebar-id';
	}
	return $sidebar;
}
// =============================================================================

Please do not forget to change the ups-sidebar-sidebar-id to the actual ID of your sidebar.

Hope this helps. Please let us know how it goes.

1 Like

Hi, I can’t get this to work.

I’ve replaced ‘ups-sidebar-sidebar-id’ with ‘ups-sidebar-category-sidebar’, the name of my sidebar, but no luck.

I’m running Icon stack, which doesn’t usually have a sidebar, so had to add the following suggested code to get it to display on my category landing pages:

function x_get_content_layout() {

$content_layout = x_get_option( 'x_layout_content' );

if ( $content_layout != 'full-width' ) {
  if ( is_home() ) {
    $opt    = x_get_option( 'x_blog_layout' );
    $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
  } elseif ( is_singular( 'post' ) ) {
    $meta   = get_post_meta( get_the_ID(), '_x_post_layout', true );
    $layout = ( $meta == 'on' ) ? 'full-width' : $content_layout;
  } elseif ( x_is_portfolio_item() ) {
    $layout = 'full-width';
  } elseif ( x_is_portfolio() ) {
    $meta   = get_post_meta( get_the_ID(), '_x_portfolio_layout', true );
    $layout = ( $meta == 'sidebar' ) ? $content_layout : $meta;
  } elseif ( is_page_template( 'template-layout-content-sidebar.php' ) ) {
    $layout = 'content-sidebar';
  } elseif ( is_page_template( 'template-layout-sidebar-content.php' ) ) {
    $layout = 'sidebar-content';
  } elseif ( is_page_template( 'template-layout-full-width.php' ) ) {
    $layout = 'full-width';
  } elseif ( is_archive() ) {
    if ( x_is_shop() || x_is_product_category() || x_is_product_tag() ) {
      $opt    = x_get_option( 'x_woocommerce_shop_layout_content' );
      $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
    } else {
      $opt    = x_get_option( 'x_archive_layout' );
      $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
    }
  } elseif ( x_is_product() ) {
    $layout = 'full-width';
  } elseif ( x_is_bbpress() ) {
    $opt    = x_get_option( 'x_bbpress_layout_content' );
    $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
  } elseif ( x_is_buddypress() ) {
    $opt    = x_get_option( 'x_buddypress_layout_content' );
    $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
  } elseif ( is_404() ) {
    $layout = 'full-width';
  } else {
    $layout = $content_layout;
  }
} else {
  $layout = $content_layout;
}

	if (x_is_product_category() ) {
     $layout = 'content-sidebar';
 }
return $layout;

  } 

Are any further customisations required here?

Thanks.

Hi There,

If Content Layout is set to Fullwidth and you don’t want to change it but you still want to display custom sidebar on search page, we have to add an template override for search page. From what you have, look for the following code near the end:

if (x_is_product_category() ) {
     $layout = 'content-sidebar';
 }

After that part add this too:

if (is_search() ) {
     $layout = 'content-sidebar';
 }

Final code will be:

function x_get_content_layout() {

$content_layout = x_get_option( 'x_layout_content' );

if ( $content_layout != 'full-width' ) {
  if ( is_home() ) {
    $opt    = x_get_option( 'x_blog_layout' );
    $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
  } elseif ( is_singular( 'post' ) ) {
    $meta   = get_post_meta( get_the_ID(), '_x_post_layout', true );
    $layout = ( $meta == 'on' ) ? 'full-width' : $content_layout;
  } elseif ( x_is_portfolio_item() ) {
    $layout = 'full-width';
  } elseif ( x_is_portfolio() ) {
    $meta   = get_post_meta( get_the_ID(), '_x_portfolio_layout', true );
    $layout = ( $meta == 'sidebar' ) ? $content_layout : $meta;
  } elseif ( is_page_template( 'template-layout-content-sidebar.php' ) ) {
    $layout = 'content-sidebar';
  } elseif ( is_page_template( 'template-layout-sidebar-content.php' ) ) {
    $layout = 'sidebar-content';
  } elseif ( is_page_template( 'template-layout-full-width.php' ) ) {
    $layout = 'full-width';
  } elseif ( is_archive() ) {
    if ( x_is_shop() || x_is_product_category() || x_is_product_tag() ) {
      $opt    = x_get_option( 'x_woocommerce_shop_layout_content' );
      $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
    } else {
      $opt    = x_get_option( 'x_archive_layout' );
      $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
    }
  } elseif ( x_is_product() ) {
    $layout = 'full-width';
  } elseif ( x_is_bbpress() ) {
    $opt    = x_get_option( 'x_bbpress_layout_content' );
    $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
  } elseif ( x_is_buddypress() ) {
    $opt    = x_get_option( 'x_buddypress_layout_content' );
    $layout = ( $opt == 'sidebar' ) ? $content_layout : $opt;
  } elseif ( is_404() ) {
    $layout = 'full-width';
  } else {
    $layout = $content_layout;
  }
} else {
  $layout = $content_layout;
}

	if (x_is_product_category() ) {
     $layout = 'content-sidebar';
 }
 if (is_search() ) {
     $layout = 'content-sidebar';
 }
return $layout;

  }

Hope this helps.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.