wordpress - 将 woocommerce-loop-product__title 从 H2 更改为 H6
问题描述
我正在尝试将 Woocommerce“woocommerce-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 - 文件覆盖
templates/content-product.php
从 WooCommerce 插件复制到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 文件中定义的includes/wc-template-functions.php
。
如果您搜索 woocommerce_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 - 使用 Hooks
另一种选择是woocommerce_template_loop_product_title
从钩子中取消钩子删除函数woocommerce_shop_loop_item_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>';
}
推荐阅读
- python - Pandas:如何基于函数使用 get_group() 和 groupby()
- android - Android没有找到打开PNG文件的意图
- python-3.x - 从在 Google Cloud Shell 中运行的 Python 打开浏览器
- sql - 如何在 SQL Server 中查找小时、分钟和秒(所有结果)的时差
- php - 从 bitbucket 历史记录中删除文件夹
- c# - SQL Server UDT 和存储过程从 .Net 代码中修剪十进制数字值
- docker - Docker ps 显示我无法停止或杀死的正在运行的容器。如何停止/杀死该容器?
- javascript - 后端(java)和前端(jsp / js)之间的集成
- c# - 从应用程序洞察(Azure 门户)获取数据以将其显示在 asp.net Web 应用程序的网页上
- konvajs - Konvajs:如果它等于 fontSize,则文本高度不会出现在 stage.toJSON() 中