As part of our sitemap (using WooCommerce), we don't want to have a page at /shop/. Instead, our menu just links directly through to the shop categories. Because of this, I want to use PHP to forward /shop/ onto our primary category, which is /product-category/coffee/.
On previous Wordpress sites I've used something similar to the below, in a dedicated page template file to forward the user on. However, using the below code inside of a template name 'page-shop.php' doesn't work, and I am left with a generic 'shop overview page' showing categories and products.
Is the code wrong or am I putting it inside the wrong template?
Thanks
<?php
/* Redirect /shop/ directly to coffee category */
wp_redirect( get_bloginfo('url').'/product-category/coffee/' ); exit;
?>
Try putting the following code into your theme's functions.php
file.
function custom_shop_page_redirect() {
if( is_shop() ){
wp_redirect( home_url( '/product-category/coffee/' ) );
exit();
}
}
add_action( 'template_redirect', 'custom_shop_page_redirect' );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With