【问题标题】:How to add a custom field text below the product title in wordpress WooCommerce [duplicate]如何在wordpress WooCommerce中的产品标题下方添加自定义字段文本[重复]
【发布时间】:2018-07-12 06:36:34
【问题描述】:

在 WooCommerce 中,我想将自定义文本添加到我的产品显示中,这将从产品编辑页面中的自定义字段中获取。

现在是这样的:

您可以在下面查看带有标题的产品:

Website link

add_action( 'woocommerce_after_shop_loop_item_title', 'custom_field_display_below_title', 2 );
function custom_field_display_below_title(){
  global $product;

  // Get the custom field value
  $custom_field = get_post_meta( $product->get_id(), '_custom_product_text_field', true );

  // Display
  if( ! empty($custom_field) ){
    echo '<p class="my-custom-field">'.$custom_field.'</p>';
  }
}

【问题讨论】:

  • 你的代码不工作吗?
  • 显示此错误;
  • 无法与站点通信以检查致命错误,因此恢复了 PHP 更改。您将需要通过其他方式上传您的 PHP 文件更改,例如使用 SFTP。
  • 您上面的代码看起来不错,我还在我的服务器上测试了您的代码。使用 ftp 或 sftp 将上述代码粘贴到当前主题 functions.php 文件的底部。

标签: php wordpress woocommerce custom-fields


【解决方案1】:

您需要将完整代码添加到主题的“functions.php”中。

// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');

// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');

function woocommerce_product_custom_fields()
{
    global $woocommerce, $post;
    echo '<div class="product_custom_field">';
    // Custom Product Text Field
    woocommerce_wp_text_input(
        array(
            'id' => '_custom_product_text_field',
            'placeholder' => 'Custom Product Text Field',
            'label' => __('Custom Product Text Field', 'woocommerce'),
            'desc_tip' => 'true'
        )
    );
    echo '</div>';
}

function woocommerce_product_custom_fields_save($post_id)
{
    // Custom Product Text Field
    $woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];
    if (!empty($woocommerce_custom_product_text_field))
        update_post_meta($post_id, '_custom_product_text_field', esc_attr($woocommerce_custom_product_text_field));
    // Custom Product Number Field
    $woocommerce_custom_product_number_field = $_POST['_custom_product_number_field'];
    if (!empty($woocommerce_custom_product_number_field))
        update_post_meta($post_id, '_custom_product_number_field', esc_attr($woocommerce_custom_product_number_field));
    // Custom Product Textarea Field
    $woocommerce_custom_procut_textarea = $_POST['_custom_product_textarea'];
    if (!empty($woocommerce_custom_procut_textarea))
        update_post_meta($post_id, '_custom_product_textarea', esc_html($woocommerce_custom_procut_textarea));
}

您的自定义字段 ID 在这里是 _custom_product_text_field,您可以在产品模板的循环中显示类似 &lt;?php echo get_post_meta($post-&gt;ID, '_custom_product_text_field', true); ?&gt; 的数据(可能会覆盖“woocommerce/single-product.php”)。

如果 WordPress 在更新“functions.php”时返回错误,请尝试通过 FTP 上传或使用一些文件管理器插件。

【讨论】:

  • 感谢您的回复!.......自定义字段显示在 WordPress 产品部分后端的价格字段下,但网站首页没有显示任何内容.....检查首页的图像...代码现在运行良好,但没有显示任何内容..
  • 您是否在标题下方使用此代码 &lt;?php echo get_post_meta($post-&gt;ID, '_custom_product_text_field', true); ?&gt; 覆盖了“woocommerce/single-product.php”?
  • 没有。我不覆盖'woocommerce/single-product.php'只需将此代码粘贴到function.php中
  • 您不应该在functions.php中粘贴&lt;?php echo get_post_meta($post-&gt;ID, '_custom_product_text_field', true); ?&gt;来显示自定义字段数据。只需从“plugins/woocommerce/templates”复制“content-product.php”,在您的主题中创建一个名为“woocommerce”的文件夹并粘贴到其中。在第 61 行附近的 do_action( 'woocommerce_after_shop_loop_item_title' ); echo get_post_meta($post-&gt;ID, '_custom_product_text_field', true); 之后添加此代码。就是这样。
  • 如果你的主题已经有这个文件,请在适当的地方添加代码,重写时要小心。此外,这将仅在产品列表部分显示自定义代码,如果您需要在详细信息页面中显示相同的代码,您应该以相同的方式覆盖“woocommerce/single-product.php”。
猜你喜欢
  • 1970-01-01
  • 2018-07-04
  • 2022-01-28
  • 2017-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-10
  • 2015-07-12
相关资源
最近更新 更多