php - 在 WooCommerce 单一产品标题下显示自定义字段

标签 php wordpress woocommerce product hook-woocommerce

我在 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>

我应该怎样做才能在标题下显示该自定义字段?
我在哪里可以找到那个钩子(Hook)?

谢谢

最佳答案

如果您查看 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 操作 Hook 中,优先级为 5 (所以它排在第一位)。

您可以通过两种方式做到这一点:

1) 您可以使用 woocommerce_single_product_summary Hook 中 Hook 的自定义函数,优先级在 69 之间,这样:

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 位于事件主题的 WooCommerce 文件夹( 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…

关于php - 在 WooCommerce 单一产品标题下显示自定义字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44548763/

相关文章:

javascript - 统计mysql中的重复数据

php - 如何在wordpress插件中包含一个html文件

php - 从 array_rand 函数的结果创建简码

php - WooCommerce 管理员订单编辑保存帖子

php - 将自定义维度字段添加到可变产品的每个变体设置

javascript - 带百分比的 JQuery Ajax 请求

php - 在 echo 中使用 php include with

php - WSF/PHP 安装错误 - Making all in savanc/bin/bash : line 17: cd: savanc: No such file or directory

html - wordpress中的居中导航菜单

php - Woocommerce WC_Order get_shipping_address() 未作为数组返回