admin管理员组文章数量:1122826
I have a custom field created for WooCommerce products.
I am using the below code to create
// ADD CUSTOM WOO DATA
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' => '_sales_email',
'placeholder' => '',
'label' => __('Sales Email Link', 'woocommerce'),
'desc_tip' => 'true'
));
echo '</div>';
}
function woocommerce_product_custom_fields_save($post_id)
{
// Custom Product Text Field
$woocommerce_custom_product_text_field = $_POST['_sales_email'];
if (!empty($woocommerce_custom_product_text_field))
update_post_meta($post_id, '_sales_email', esc_attr($woocommerce_custom_product_text_field));
}
The Code works well, and displays the data on the product page.
I am displaying the data via a shortcode.
<div class="dbtn_sales"><a href="mailto:[foobar name=_sales_email]">Email Sales</a></div>
The problem I am having is hiding the data if the field is empty..
I have tried
if( get_field('_sales_email') )
{
//echo the_field('_sales_email');
}
else
{
echo "<style>.dbtn_sales{display:none !important;}</style>";
}
but I'm getting nowhere... Can anyone point me in the right direction here?
I have a custom field created for WooCommerce products.
I am using the below code to create
// ADD CUSTOM WOO DATA
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' => '_sales_email',
'placeholder' => '',
'label' => __('Sales Email Link', 'woocommerce'),
'desc_tip' => 'true'
));
echo '</div>';
}
function woocommerce_product_custom_fields_save($post_id)
{
// Custom Product Text Field
$woocommerce_custom_product_text_field = $_POST['_sales_email'];
if (!empty($woocommerce_custom_product_text_field))
update_post_meta($post_id, '_sales_email', esc_attr($woocommerce_custom_product_text_field));
}
The Code works well, and displays the data on the product page.
I am displaying the data via a shortcode.
<div class="dbtn_sales"><a href="mailto:[foobar name=_sales_email]">Email Sales</a></div>
The problem I am having is hiding the data if the field is empty..
I have tried
if( get_field('_sales_email') )
{
//echo the_field('_sales_email');
}
else
{
echo "<style>.dbtn_sales{display:none !important;}</style>";
}
but I'm getting nowhere... Can anyone point me in the right direction here?
Share Improve this question edited Jan 14, 2019 at 13:50 Krzysiek Dróżdż 25.5k9 gold badges53 silver badges74 bronze badges asked Jan 14, 2019 at 13:38 SoutherndavisSoutherndavis 114 bronze badges1 Answer
Reset to default 0AFAIR get_field()
function comes with ACF and there is no such function built in WP.
It isn't obvious, based on your code, that you use ACF plugin on your site, so there is a chance it might not work.
But, since you already use native WP function for setting the meta data, then it would be good idea to use native WP function to get that value as well.
Also, I wouldn't hide that button using CSS. I would change your code (part that is responsible for printing the button) to this:
<?php if ( get_post_meta(get_the_ID(), '_sales_email', true) ) : ?>
<div class="dbtn_sales"><a href="mailto:[foobar name=_sales_email]">Email Sales</a></div>
<?php endif; ?>
Also... If that value comes from user input, then it would be a good idea to assure it is properly escaped:
<?php if ( get_post_meta(get_the_ID(), '_sales_email', true) ) : ?>
<div class="dbtn_sales"><a href="mailto:<?php esc_attr( get_post_meta(get_the_ID(), '_sales_email', true) ); ?>">Email Sales</a></div>
<?php endif; ?>
本文标签: Hide custom meta data if empty
版权声明:本文标题:Hide custom meta data if empty 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736285536a1927479.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论