Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在单个产品页面的“简短说明”下显示自定义字段_Php_Wordpress_Woocommerce_Custom Fields_Meta Boxes - Fatal编程技术网

Php 在单个产品页面的“简短说明”下显示自定义字段

Php 在单个产品页面的“简短说明”下显示自定义字段,php,wordpress,woocommerce,custom-fields,meta-boxes,Php,Wordpress,Woocommerce,Custom Fields,Meta Boxes,在woocommerce中,我使用一些代码在产品编辑页面中添加带有自定义字段的Metabox 如何在单个产品页面的简短描述下显示此自定义字段的值 这是我的密码: add_action ('add_meta_boxes','add_info_meta_box'); function add_info_meta_box() { add_meta_box('new_meta', 'info','info_meta_fields_output','product', 'side'); } f

在woocommerce中,我使用一些代码在产品编辑页面中添加带有自定义字段的Metabox

如何在单个产品页面的简短描述下显示此自定义字段的值

这是我的密码:

add_action ('add_meta_boxes','add_info_meta_box');
function add_info_meta_box()
{
    add_meta_box('new_meta', 'info','info_meta_fields_output','product', 'side');
}


function info_meta_fields_output($post)
{
    $new_meta = get_post_meta($post->ID,'_new_meta',true);
    echo ('<label for="new_meta"> Custom Text </label>');
    echo ('<input type="text" id="new_meta" name="new_meta" value="'.esc_attr($new_meta).'"/>');
}

add_action('save_post','save_info_meta_box');
function save_info_meta_box($post_id)
{
    $new_meta=sanitize_text_field($_POST['new_meta']);
    update_post_meta ($post_id,'_new_meta',$new_meta);
}


// Displaying the value on single product pages
function meta_product($product_id) {

    $new_meta2 = get_post_meta(get_the_ID(),'_new_meta', true);
    echo ('<p id="value-on-single-product">' . $new_meta2 . '</p>');
}
add_action('woocommerce_single_product_summary', 'meta_product',30);
add_action('add_meta_box'、'add_info_meta_box');
函数添加信息元框()
{
添加元框(“新元”、“信息”、“信息元”、“字段输出”、“产品”、“侧面”);
}
函数信息\元\字段\输出($post)
{
$new\u meta=get\u post\u meta($post->ID,“new\u meta”,true);
回声(‘自定义文本’);
回声(“”);
}
添加操作(“保存帖子”、“保存信息”元框);
函数保存信息元框($post\u id)
{
$new\u meta=清理文本字段($\u POST['new\u meta']);
更新发布元数据($post\u id,“'new\u meta',$new\u meta”);
}
//在单个产品页面上显示值
功能元产品($product\U id){
$new\u meta2=get\u post\u meta(get\u ID(),“new\u meta”,true);
echo(“

”.$new\u meta2.

”); } 添加行动(“商业单产品摘要”、“元产品”,30);
但它不显示自定义字段值。

更新了(根据评论中的要求添加了第二个自定义字段)

您应该尝试以下操作,这将在“产品常规”选项卡“元数据库”中设置自定义字段,并在“产品简短说明”下显示此自定义字段值:

// Add the custom field
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_field_to_general_product_metabox' );
function add_custom_field_to_general_product_metabox() {
    global $post;

    // Get the selected value
    $value = get_post_meta( $post->ID, '_new_meta', true );
    if( empty( $value ) ) $value = ''; // Default value

    woocommerce_wp_text_input( array(
        'id'       => 'new_meta',
        'label'    => __( 'Thông tin thêm', 'woocommerce' ),
        'placeholder'       => __( '', 'woocommerce' ),
        'description'       => __( '', 'woocommerce' ),
        'value'   => $value, // Displaying the selected value
    ) );


    // Get the selected value
    $value2 = get_post_meta( $post->ID, '_new_meta2', true );
    if( empty( $value2 ) ) $value2 = ''; // Default value

    woocommerce_wp_text_input( array(
        'id'       => 'new_meta2',
        'label'    => __( 'Thông tin thêm', 'woocommerce' ),
        'placeholder'       => __( '', 'woocommerce' ),
        'description'       => __( '', 'woocommerce' ),
        'value'   => $value2, // Displaying the selected value
    ) );
}

// Save the custom field
add_action( 'woocommerce_process_product_meta', 'save_custom_field_to_general_product_metabox' );
function save_custom_field_to_general_product_metabox( $post_id ){

    if( isset( $_POST['new_meta'] ) )
        update_post_meta( $post_id, '_new_meta', esc_attr( $_POST['new_meta'] ) );

    if( isset( $_POST['new_meta2'] ) )
        update_post_meta( $post_id, '_new_meta2', esc_attr( $_POST['new_meta2'] ) );
}


// Displaying the custom field value (on single product pages under short description)
add_action('woocommerce_single_product_summary', 'display_custom_meta_field_value', 25 );
function display_custom_meta_field_value() {
    global $product;

    $custom_field = get_post_meta( $product->get_id(),'_new_meta', true );
    if( ! empty( $custom_field ) )
        echo  '<p id="value-on-single-product">' . $custom_field . '</p>';

    $custom_field2 = get_post_meta( $product->get_id(),'_new_meta2', true );
    if( ! empty( $custom_field2 ) )
        echo '<p id="value-on-single-product">' . $custom_field2 . '</p>';
}
//添加自定义字段
添加操作('woocommerce\u product\u options\u general\u product\u data','add\u custom\u field\u to\u general\u product\u metabox');
函数添加\自定义\字段\到\常规\产品\元框(){
全球$员额;
//获取所选的值
$value=get_post_meta($post->ID,'u new_meta',true);
if(空($value))$value='';//默认值
woocommerce_wp_text_输入(数组(
'id'=>'new\u meta',
“标签”=>uuuu('Thôtin Thêm','woocommerce'),
“占位符”=>uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
'description'=>uuu('woocommerce'),
'value'=>$value,//显示所选值
) );
//获取所选的值
$value2=get_post_meta($post->ID,'u new_meta2',true);
if(空($value2))$value2='';//默认值
woocommerce_wp_text_输入(数组(
'id'=>'new_meta2',
“标签”=>uuuu('Thôtin Thêm','woocommerce'),
“占位符”=>uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
'description'=>uuu('woocommerce'),
'value'=>$value2,//显示所选值
) );
}
//保存自定义字段
添加操作(“woocommerce\u process\u product\u meta”、“save\u custom\u field\u to\u general\u product\u metabox”);
函数保存\自定义\字段\到\常规\产品\元数据库($post\ id){
如果(isset($\u POST['new\u meta']))
更新发布元数据($发布id,''new''meta',esc''attr($'post['new']);
如果(isset($\u POST['new\u meta2']))
更新发布元数据($发布id,''.'新发布元数据2',esc_属性($.'发布['新发布元数据2']);
}
//显示自定义字段值(在“简短说明”下的单个产品页面上)
添加操作('woocommerce\u single\u product\u summary','display\u custom\u meta\u field\u value',25);
函数显示\自定义\元\字段\值(){
全球$产品;
$custom\u field=get\u post\u meta($product->get\u id(),“'u new\u meta',true);
如果(!空($custom_字段))
echo'

。$custom_字段。

'; $custom_field2=get_post_meta($product->get_id(),“'u new_meta2',true); 如果(!空($custom_field2)) echo'

。$custom_field2.

; }
代码进入活动子主题(或活动主题)的function.php文件


已测试并正常工作。

您好,欢迎来到StackOverflow。请花些时间阅读帮助页面,特别是命名和的部分。更重要的是,请阅读。你可能还想了解。嗨,太专业了。如何在单个产品上隐藏id“p id=”值“当没有输入数据时,您可以添加一行数据以显示更多信息。谢谢。您好,我正在使用此代码显示2个自定义字段。可以将二者合并为一,非常感谢。我使用的是分离,你能帮我吗?//添加自定义字段1和//添加自定义字段2我不知道如何快速包含2个自定义字段1,谢谢。我通过gmail发送了我的表情,但答案是“帮助”。非常感谢。下周我会把你捐出去。