【问题标题】:Add product title heading tag for WooCommerce category archive pages为 WooCommerce 类别存档页面添加产品标题标题标签
【发布时间】:2020-10-31 13:50:37
【问题描述】:

我希望更改产品类别页面上显示的产品的标题标签(从无标题标签更改为 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。通过在 woocommerce-loop-product 的 functions.php 文件中添加代码

它可以工作,但它会随处更改标题标签,即使对于显示在主页上的产品也是如此。

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 文件中添加代码,标题标签的更改不适用于相关产品?

我从来没有学过编码,我在这里被阻止了。

【问题讨论】:

    标签: php wordpress woocommerce product html-heading


    【解决方案1】:

    您可以使用条件标签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文件arround woocommerce_shop_loop_item_title hook中添加代码

    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>';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 2019-06-23
      • 1970-01-01
      相关资源
      最近更新 更多