首页 文章

将WooCommerce 'Shop'页面转发到网站上的另一个页面

提问于
浏览
4

作为我们站点 Map 的一部分(使用WooCommerce),我们不希望在/ shop /中有一个页面 . 相反,我们的菜单只是直接链接到商店类别 . 因此,我想使用PHP转发/购买/进入我们的主要类别,即/ product-category / coffee / .

在以前的Wordpress网站上,我使用了类似于下面的内容,在专用页面模板文件中转发用户 . 但是,在模板名称'page-shop.php'中使用以下代码不起作用,我留下了一个显示类别和产品的通用“商店概述页面” .

代码是错误的还是我把它放在错误的模板中?

谢谢

<?php
/* Redirect /shop/ directly to coffee category */
wp_redirect( get_bloginfo('url').'/product-category/coffee/' ); exit;
?>

2 回答

  • 1

    尝试将以下代码放入主题的 functions.php 文件中 .

    function custom_shop_page_redirect() {
        if( is_shop() ){
            wp_redirect( home_url( '/product-category/coffee/' ) );
            exit();
        }
    }
    add_action( 'template_redirect', 'custom_shop_page_redirect' );
    
  • 8

    来自@Khorshed的答案略有改进

    function custom_shop_page_redirect() {
        if( is_shop() ){
            $product_cat_id = 8;
            $link = get_term_link( $product_cat_id, 'product_cat' );
            wp_redirect( $link );
            exit();
        }
    }
    add_action( 'template_redirect', 'custom_shop_page_redirect' );
    

相关问题