首页 > 解决方案 > How to redirect WooCommerce shop to a custom shop page

问题描述

I have a website built on the Ample Business theme with the Woocommerce plugin installed and activated. We have created a custom shop page and want to redirect the Woocommerce /shop/ URL to the new page. I have read several posts that prescribe answers, but none seem to work. The page will not redirect.

What I've tried:

Added the following code to the child theme's functions.php file:

    // Redirect WooCommerce Shop URL 
    function wpc_shop_url_redirect() 
    { if( is_shop() ){ 
    wp_redirect( home_url( '/shop2/' ) ); // Assign custom internal page here exit(); } } 
    add_action( 'template_redirect', 'wpc_shop_url_redirect' );

Tried a standard 301 redirect via .htaccess.

These solutions do not work and I can not simply create a new template file for archive_product.php because all Woocommerce files reside in the plugins folder as opposed to the theme folder. I do not want the file overwritten with each update.

Any suggestions would be greatly appreciated.

标签: phpwordpresswoocommerce

解决方案


首先,创建一个shop2页面。然后创建页面模板,然后将 WordPress 页面模板分配给页面shop2页面。

下面是示例模板文件的样子。你可以给这个文件名template-shop2.php

<?php 

    /* Template Name: Example Template */ 

    get_header();

    // your product display code.

    get_footer();

?>

然后你可以使用你的template_redirect并重定向到你的shop2. 此代码将进入您的活动主题 functions.php 文件。

function custom_shop_page_redirect() {
    if( is_shop() ){
        wp_redirect( home_url( '/shop2/' ) );
        exit();
    }
}
add_action( 'template_redirect', 'custom_shop_page_redirect' );

推荐阅读