You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改特定分类商品在WooCommerce店铺及归档页的链接

Got it, let's break this down clearly. You already know we need to remove the default product link hooks, but the key part is replacing them with your custom link only for specific product categories—here's exactly how to do it:

First, we'll unhook WooCommerce's default link functions so we can replace them with our own. Add this to your child theme's functions.php file (always use a child theme to avoid losing changes when updating your main theme):

// Remove default product link markup from shop/archive pages
remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );

Next, we'll add our own hooks to output a custom link for your specific categories, and fall back to the default link for all other products. Here's the code:

// Output custom product link opening tag
add_action( 'woocommerce_before_shop_loop_item', 'custom_product_link_open', 10 );
function custom_product_link_open() {
    global $product;
    
    // Replace these slugs with your target product category slugs
    $target_categories = array( 'electronics', 'smart-home' );
    
    // Check if the current product is in any of the target categories
    if ( has_term( $target_categories, 'product_cat', $product->get_id() ) ) {
        // Replace this with your custom URL—can be static or dynamic
        // Example dynamic URL: $product->get_permalink() . 'custom-section/'
        $custom_url = 'https://your-custom-destination.com';
        
        // Output the custom link with WooCommerce's default class for styling
        echo '<a href="' . esc_url( $custom_url ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
    } else {
        // Fallback: use the default product permalink for non-target categories
        echo '<a href="' . esc_url( $product->get_permalink() ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
    }
}

// Output the closing link tag (required for both custom and default links)
add_action( 'woocommerce_after_shop_loop_item', 'custom_product_link_close', 5 );
function custom_product_link_close() {
    echo '</a>';
}

Key Customization Tips

  • Target Multiple Categories: Update the $target_categories array with your actual product category slugs (you can find these in the WordPress admin under Products → Categories, look for the "Slug" field).
  • Dynamic Custom Links: If you need a unique link per product (e.g., linking to a custom landing page for each item), modify the $custom_url line to use product data. For example:
    $custom_url = 'https://your-site.com/custom-product-page/' . $product->get_slug();
    
  • Different Links Per Category: If each target category needs its own custom destination, use conditional checks like this:
    if ( has_term( 'electronics', 'product_cat', $product->get_id() ) ) {
        $custom_url = 'https://your-electronics-store.com';
    } elseif ( has_term( 'smart-home', 'product_cat', $product->get_id() ) ) {
        $custom_url = 'https://your-smart-home-shop.com';
    } else {
        $custom_url = $product->get_permalink();
    }
    

Final Notes

  • Always test the code on a staging site first to avoid breaking your live store.
  • Make sure to clear your WooCommerce and browser cache after adding the code to see the changes.

内容的提问来源于stack exchange,提问作者Mohsen Jalalian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:40:38