Archived topic
Sidebar only on shop page and underlying pages
9 replies · Started by Eric on September 5, 2017
Hi,
I would like to have a sidebar only on the shop page and all underlying pages. I used this code below but it only applies to the shop page itself, while I also like it to apply to eliv.com.tw/shop/etc (but not all WooCommerce pages like eliv.com.tw/product/etc.). Do you know how this code has to be adjusted to make it work?
add_filter( 'generate_sidebar_layout','tu_custom_woocommerce_sidebar_layout' );
function tu_custom_woocommerce_sidebar_layout( $layout )
{
$shoppage = "/shop/";
$currentpage = $_SERVER['REQUEST_URI'];
// If we are on a woocommerce page, set the sidebar
if($currentpage == $shoppage) return 'right-sidebar';
// Or else, set the regular layout
return $layout;
}
Best regards,
Florian
Hi there,
Can you try the first function here? https://docs.generatepress.com/article/sidebar-layout/#using-a-function
Yes I tried the WooCommerce function from there but that one also enables the sidebar on product pages (which I don't want).
Hmm try this:
add_filter( 'generate_sidebar_layout','tu_custom_woocommerce_sidebar_layout' );
function tu_custom_woocommerce_sidebar_layout( $layout )
{
// If we are on a woocommerce page, set the sidebar
if ( function_exists( 'is_woocommerce' ) && is_woocommerce() && !is_shop() )
return 'both-left';
// Or else, set the regular layout
return $layout;
}
Hi Leo,
Thanks for your reply, however, this doesn't work and doesn't return a sidebar (also when I replace both-left with right-sidebar).
Best regards,
Florian
What about this?:
add_filter( 'generate_sidebar_layout','tu_custom_woocommerce_sidebar_layout' );
function tu_custom_woocommerce_sidebar_layout( $layout ) {
// If we are on a woocommerce page, set the sidebar
if ( function_exists( 'is_woocommerce' ) && is_woocommerce() && ! is_singular( 'product' ) )
return 'right-sidebar';
// Or else, set the regular layout
return $layout;
}
That will set the right sidebar to any WC related page, other than single products.
Otherwise, it will use the Customizer setting.
Hi Tom,
When I put this code in functions.php replacing the old one, it simply gives a completely white page on all WooCommerce pages.
Best regards,
Florian
Ah, sorry about that. Just updated the code so it should work now.
That's awesome, it worked!
Thanks
You're welcome :)