需要在单个产品页面上显示相关产品中的自定义字段。
我想添加一个 meta框到Add New Product
字段,并在自定义选项卡上显示结果的单一产品页面的评论。我尝试使用代码,但没有显示在页面上。添加额外的产品选项卡帮助我添加额外的信息。所以添加,保存和显示产品是我正在寻找的。
add_filter( 'add_meta_boxes', 'bhww_core_cpt_metaboxes' );
function bhww_core_cpt_metaboxes( $meta_boxes ) {
//global $prefix;
$prefix = '_bhww_'; // Prefix for all fields
// Add metaboxes to the 'Product' CPT
$meta_boxes[] = array(
'id' => 'bhww_woo_tabs_metabox',
'title' => 'Additional Product Information - <strong>Optional</strong>',
'pages' => array( 'product' ), // Which post type to associate with?
'context' => 'normal',
'priority' => 'default',
'show_names' => true,
'fields' => array(
array(
'name' => __( 'Ingredients', 'cmb' ),
'desc' => __( 'Anything you enter here will be displayed on the Ingredients tab.', 'cmb' ),
'id' => $prefix . 'ingredients_wysiwyg',
'type' => 'wysiwyg',
'options' => array( 'textarea_rows' => 5, ),
),
array(
'name' => __( 'Benefits', 'cmb' ),
'desc' => __( 'Anything you enter here will be displayed on the Benefits tab.', 'cmb' ),
'id' => $prefix . 'benefits_wysiwyg',
'type' => 'wysiwyg',
'options' => array( 'textarea_rows' => 5, ),
),
),
);
return $meta_boxes;
}
/////////////////////////////////////////
add_filter( 'woocommerce_product_data_tabs', 'bhww_woo_extra_tabs' );
function bhww_woo_extra_tabs( $tabs1 ) {
global $post;
$product_ingredients = get_post_meta( $post->ID, '_bhww_ingredients_wysiwyg', true );
$product_benefits = get_post_meta( $post->ID, '_bhww_benefits_wysiwyg', true );
if ( ! empty( $product_ingredients ) ) {
$tabs1['ingredients_tab'] = array(
'title' => __( 'Ingredients', 'woocommerce' ),
'priority' => 15,
'callback' => 'bhww_woo_ingredients_tab_content'
);
}
if ( ! empty( $product_benefits ) ) {
$tabs1['benefits_tab'] = array(
'title' => __( 'Benefits', 'woocommerce' ),
'priority' => 16,
'callback' => 'bhww_woo_benefits_tab_content'
);
}
return $tabs1;
}
function bhww_woo_ingredients_tab_content() {
global $post;
$product_ingredients = get_post_meta( $post->ID, '_bhww_ingredients_wysiwyg', true );
if ( ! empty( $product_ingredients ) ) {
echo '<h2>' . esc_html__( 'Product Ingredients', 'woocommerce' ) . '</h2>';
// Updated to apply the_content filter to WYSIWYG content
echo apply_filters( 'the_content', $product_ingredients );
}
}
function bhww_woo_benefits_tab_content() {
global $post;
$product_benefits = get_post_meta( $post->ID, '_bhww_benefits_wysiwyg', true );
if ( ! empty( $product_benefits ) ) {
echo '<h2>' . esc_html__( 'Product Benefits', 'woocommerce' ) . '</h2>';
// Updated to apply the_content filter to WYSIWYG content
echo apply_filters( 'the_content', $product_benefits );
}
}
1条答案
按热度按时间i1icjdpr1#
以下是在管理员编辑产品页面中添加2个自定义字段(编辑器所见即所得)并在前端单个产品页面自定义选项卡中显示值的方法:
经过测试并有效。您将获得:
1)后端:
2)在前端: