Reputation: 77
I am displaying some custom fields on the woocommerce single product page with this
add_action( 'woocommerce_single_product_summary','add_custom_field', 20 );
function add_custom_field() {
global $post;
echo get_post_meta( $post->ID, 'Brand', true );
echo get_post_meta( $post->ID, 'Content', true );
return true;
}
This dispays only the values of the custom fields, but I would like the names before so it would look like this:
Brand: ...
Content: ...
The custom fields don't aplly to every product though, so for the products where the custom fields are not set, nothing should be displayed.
Upvotes: 1
Views: 183
Reputation: 4880
Try below code
add_action( 'woocommerce_single_product_summary', 'add_custom_field', 20 );
function add_custom_field() {
global $post;
$brand = get_post_meta( $post->ID, 'Brand', true );
$content = get_post_meta( $post->ID, 'Content', true );
if (!empty($brand)) {
echo 'Brand: '. $brand .'<br>';
}
if (!empty($content)) {
echo 'Content: '. $content .'<br>';
}
}
Upvotes: 0
Reputation: 137
Use this:
add_action( 'woocommerce_single_product_summary', 'add_custom_field', 20 );
function add_custom_field() {
global $post;
$brand = get_post_meta( $post->ID, 'Brand', true );
$content = get_post_meta( $post->ID, 'Content', true );
if (!empty($brand)) {
echo 'Brand: '. $brand;
}
if (!empty($content)) {
echo 'Content: '. $content;
}
}
Upvotes: 3