将woommercence-lop-product__title从H2更改为H6



我正试图将Wooccommerce的"wooccommerce-loop-product__title"从H2更改为H6,但我在查找函数时遇到了一些问题。

有人能就插件文件中的位置提出建议吗?或者更好的是,如何在主题functions.php文件中覆盖它?

感谢

有两种方法可以做到这一点-使用钩子,或者覆盖Child主题中的WooCommerce模板文件。首先,让我们找到代码。

你要找的文件在WooCommerce插件中:

templates/content-product.php

该文件的第5行写道:

This template can be overridden by copying it to yourtheme/woocommerce/content-product.php.

让我们先来看看文件覆盖。

方法1-文件覆盖

将WooCommerce插件中的templates/content-product.php复制到Child主题内的woocommerce/content-product.php。该文件现在正在覆盖插件中的模板。我们对这个新文件进行了所需的编辑。

默认content-product.php模板文件中的标题输出如下:

echo '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</h2>';

其在WooCommerce文件CCD_ 4中定义。

如果您搜索woommerce_template_loop_product_title((,您将看到定义的函数:

if ( ! function_exists( 'woocommerce_template_loop_product_title' ) ) {
/**
* Show the product title in the product loop. By default this is an H2.
*/
function woocommerce_template_loop_product_title() {
echo '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</h2>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}

content-product.php文件中的这行代码:

do_action( 'woocommerce_shop_loop_item_title' );

调用函数woocommerce_template_loop_product_title,这是我们想要覆盖的函数。因此,让我们注释掉这一行,并将其替换为您的代码:

// do_action( 'woocommerce_shop_loop_item_title' );
echo '<h6 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</h6>';

简单!

方法2-使用挂钩

另一个选项是从woocommerce_shop_loop_item_title挂钩中取消挂钩removewoocommerce_template_loop_product_title函数,并将其替换为我们自己的函数。您可以通过在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 '<h6 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</h6>';
}

最新更新