在 WooCommerce 单个产品标题下显示自定义字段
Display a custom field under WooCommerce single product title
我对 WooCommerce 中的自定义字段有疑问。
我有产品(书籍)的标题,我想添加带有自定义字段的作者。我通过在主题自定义字段中注册来完成它,并使用 Wordpress 自定义字段添加新的。
自定义字段调用:product_author
(这是我的)和mbt_publisher_name
(来自主题)
我在主题和 woocommerce 目录中找到了模板文件 title.php
。
我尝试添加:
<?php echo get_post_meta($id, "product_author", true); ?>
没有任何变化...
来自 title.php
的原始来源
<?php
/**
* Single Product title
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<h2 itemprop="name" class="product_title entry-title"><?php the_title(); ?></h2>
我应该怎么做才能在标题下显示该自定义字段?
我在哪里可以找到那个钩子?
谢谢
如果您查看 woocommerce 模板 content-single-product.php
,您将看到此代码 (从第 54 行开始):
/**
* woocommerce_single_product_summary hook.
*
* @hooked woocommerce_template_single_title - 5
* @hooked woocommerce_template_single_rating - 10
* @hooked woocommerce_template_single_price - 10
* @hooked woocommerce_template_single_excerpt - 20
* @hooked woocommerce_template_single_add_to_cart - 30
* @hooked woocommerce_template_single_meta - 40
* @hooked woocommerce_template_single_sharing - 50
* @hooked WC_Structured_Data::generate_product_data() - 60
*/
do_action( 'woocommerce_single_product_summary' );
因此 woocommerce_template_single_title
挂钩在 woocommerce_single_product_summary
优先级为 的动作挂钩中5
(所以它在前).
您可以通过两种方式完成:
1) 您可以使用挂钩在 woocommerce_single_product_summary
挂钩中的自定义函数,优先级在 6 到 [=43 之间=]9,这样:
add_action( 'woocommerce_single_product_summary', 'custom_action_after_single_product_title', 6 );
function custom_action_after_single_product_title() {
global $product;
$product_id = $product->get_id(); // The product ID
// Your custom field "Book author"
$book_author = get_post_meta($product_id, "product_author", true);
// Displaying your custom field under the title
echo '<p class="book-author">' . $book_author . '</p>';
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并适用于 WooCommerce 3.0+
或
2) 可以直接编辑模板single-product/title.php
located in the WooCommerce folder of your active theme (see below the reference about overriding WooCommerce templates through theme):
<?php
/**
* Single Product title
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
// Calling global WC_Product object
global $product;
$product_id = $product->get_id(); // The product ID
// Your custom field "Book author"
$book_author = get_post_meta($product_id, "product_author", true);
?>
<h2 itemprop="name" class="product_title entry-title"><?php the_title(); ?></h2>
<p class="book-author"><?php echo $book_author; ?></p>
官方参考:Template Structure + Overriding WooCommerce Templates via a Theme
I recommend you the first method because it's cleaner as it uses a hook and you will not need to make any changes if templates are updated. You should also better use a child theme…
我对 WooCommerce 中的自定义字段有疑问。
我有产品(书籍)的标题,我想添加带有自定义字段的作者。我通过在主题自定义字段中注册来完成它,并使用 Wordpress 自定义字段添加新的。
自定义字段调用:product_author
(这是我的)和mbt_publisher_name
(来自主题)
我在主题和 woocommerce 目录中找到了模板文件 title.php
。
我尝试添加:
<?php echo get_post_meta($id, "product_author", true); ?>
没有任何变化... 来自 title.php
的原始来源 <?php
/**
* Single Product title
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<h2 itemprop="name" class="product_title entry-title"><?php the_title(); ?></h2>
我应该怎么做才能在标题下显示该自定义字段?
我在哪里可以找到那个钩子?
谢谢
如果您查看 woocommerce 模板 content-single-product.php
,您将看到此代码 (从第 54 行开始):
/**
* woocommerce_single_product_summary hook.
*
* @hooked woocommerce_template_single_title - 5
* @hooked woocommerce_template_single_rating - 10
* @hooked woocommerce_template_single_price - 10
* @hooked woocommerce_template_single_excerpt - 20
* @hooked woocommerce_template_single_add_to_cart - 30
* @hooked woocommerce_template_single_meta - 40
* @hooked woocommerce_template_single_sharing - 50
* @hooked WC_Structured_Data::generate_product_data() - 60
*/
do_action( 'woocommerce_single_product_summary' );
因此 woocommerce_template_single_title
挂钩在 woocommerce_single_product_summary
优先级为 的动作挂钩中5
(所以它在前).
您可以通过两种方式完成:
1) 您可以使用挂钩在 woocommerce_single_product_summary
挂钩中的自定义函数,优先级在 6 到 [=43 之间=]9,这样:
add_action( 'woocommerce_single_product_summary', 'custom_action_after_single_product_title', 6 );
function custom_action_after_single_product_title() {
global $product;
$product_id = $product->get_id(); // The product ID
// Your custom field "Book author"
$book_author = get_post_meta($product_id, "product_author", true);
// Displaying your custom field under the title
echo '<p class="book-author">' . $book_author . '</p>';
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并适用于 WooCommerce 3.0+
或
2) 可以直接编辑模板single-product/title.php
located in the WooCommerce folder of your active theme (see below the reference about overriding WooCommerce templates through theme):
<?php
/**
* Single Product title
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
// Calling global WC_Product object
global $product;
$product_id = $product->get_id(); // The product ID
// Your custom field "Book author"
$book_author = get_post_meta($product_id, "product_author", true);
?>
<h2 itemprop="name" class="product_title entry-title"><?php the_title(); ?></h2>
<p class="book-author"><?php echo $book_author; ?></p>
官方参考:Template Structure + Overriding WooCommerce Templates via a Theme
I recommend you the first method because it's cleaner as it uses a hook and you will not need to make any changes if templates are updated. You should also better use a child theme…