簡體   English   中英

為 WooCommerce 類別存檔頁面添加產品標題標簽

[英]Add product title heading tag for WooCommerce category archive pages

我希望更改產品類別頁面上顯示的產品的標題標簽(從無標題標簽到 H2),但僅在產品類別頁面上

我找到了 2 個可行的解決方案,但是它們也會更改其他頁面上顯示的產品的標題標簽,這是我不想要的。

1. 通過更改 content-product.php 文件中的標題標簽

它可以工作,但是也會更改產品頁面上相關產品中顯示的產品。

<div class="box-text <?php echo flatsome_product_box_text_class(); ?>">
    <?php
        do_action( 'woocommerce_before_shop_loop_item_title' );

        echo '<h2 class="title-wrapper" style="font-size: 100%">';
        do_action( 'woocommerce_shop_loop_item_title' );
        echo '</h2>';

2.通過在functions.php文件中為woocommerce-loop-product添加代碼

它可以工作,但它會隨處更改標題標簽,即使是主頁上顯示的產品也是如此。

remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
add_action('woocommerce_shop_loop_item_title', 'soChangeProductsTitle', 10 );
function soChangeProductsTitle() {
    echo '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</h2>';
}

也許第一個解決方案是最好的,然后我只需要在 related.php 文件或 funtions.php 文件中添加代碼,標題標簽的更改不適用於相關產品?

我從來沒有學過編碼,我被擋在這里了。

您可以使用條件標簽is_product_category()如下:

1. 通過更改 content-product.php 文件中的標題標簽

<div class="box-text <?php echo flatsome_product_box_text_class(); ?>">
<?php
    do_action( 'woocommerce_before_shop_loop_item_title' );
    
    if( is_product_category() ) { 
        echo '<h2 class="title-wrapper" style="font-size: 100%">';
    }

    do_action( 'woocommerce_shop_loop_item_title' );

    if( is_product_category() ) {
         echo '</h2>';
    }

2. 或者通過在functions.php文件中添加代碼woocommerce_shop_loop_item_title鈎子

add_action('woocommerce_shop_loop_item_title', 'product_heading_title_on_category_archives', 1 );
function product_heading_title_on_category_archives() {
    if( is_product_category() ) { 
        remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
        add_action('woocommerce_shop_loop_item_title', 'change_product_heading_title', 10 );
    }
}
function change_product_heading_title() {
    echo '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</h2>';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM