【问题标题】:Display a custom field under WooCommerce single product title在 WooCommerce 单个产品标题下显示自定义字段
【发布时间】:2017-11-16 20:11:39
【问题描述】:

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

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

谢谢

【问题讨论】:

    标签: php wordpress woocommerce product hook-woocommerce


    【解决方案1】:

    如果您查看 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_titlewoocommerce_single_product_summary 动作钩子钩住,优先级为 5 (所以它来了首先)。

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

    1) 您可以使用 woocommerce_single_product_summary 挂钩中的自定义函数,其优先级介于 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

    我向您推荐第一种方法,因为它使用挂钩更简洁,并且如果更新模板,您无需进行任何更改。您还应该更好地使用子主题...

    【讨论】:

    • 第二种方法是正确的:) 很好...非常感谢...我可以再问你一个问题吗?我想创建指向所有作者书籍的链接......有可能吗?我在带有底部的主题中有一些功能(称为“由...'作者'添加,就像用户不是书的作者一样)但是在错误的地方(低于价格 - 我找不到它在哪里)并且 URL 显示 404错误页面。
    • 再次感谢 @LoicTheAztec 帮助我们了解 WooCommerce 架构。
    猜你喜欢
    • 2020-12-04
    • 2021-04-28
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 2021-12-13
    • 2021-04-12
    相关资源
    最近更新 更多