php - 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.
解决方案
首先,创建一个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' );
推荐阅读
- python - 缩短用户输入的睡眠时间
- python - 如何绘制出版物数量与年份的关系。有没有使用 python 或 Bibtex 的方法?
- c++ - c++ ReleaseSemaphore返回无效句柄错误
- azure - 如何将 azure app 服务集成到 keyvault
- javascript - Node.JS 获取/发布
- mysql - 在表中添加外键的麻烦
- sql - 语法错误接近或大小写 - postgresql
- arrays - Laravel:在数组中
- javascript - 使用 v-for 在 vuejs 中对 HTML 元素进行分组
- javascript - JavaScript 中的条件和表达式有什么区别?