admin管理员组

文章数量:1289635

Please help me with the following question.

I want to display an image in an additional TAB on the single product page (THIS PART WORKS ;-)). Sofar i can display the TAB and the image that has an URL on the product regular data (added field in the product adminstration. custom field = _product_tech)

BUT NOW: how can i check the custom field ? When it has no value it should display a placeholder image or better, a shortcode.

this is my code:

add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
        function woo_new_product_tab( $tabs ) {
        // Adds the new tab

            $tabs['desc_tab'] = array(
                'title'     => __( 'Technische informatie', 'woocommerce' ),
                'priority'  => 50,
                'callback'  => 'woo_new_product_tab_content'
            );
        return $tabs;   
        }
        
function woo_new_product_tab_content()  {
            // The new tab content

            $prod_id = get_the_ID();
            echo'<a class="shutter" title="technische gegevens" href="'.get_post_meta($prod_id,'_product_tech',true).'"><img width="750" height="100%" src="'.get_post_meta($prod_id,'_product_tech',true).'"></a>';
        
        }

Please help me with the following question.

I want to display an image in an additional TAB on the single product page (THIS PART WORKS ;-)). Sofar i can display the TAB and the image that has an URL on the product regular data (added field in the product adminstration. custom field = _product_tech)

BUT NOW: how can i check the custom field ? When it has no value it should display a placeholder image or better, a shortcode.

this is my code:

add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
        function woo_new_product_tab( $tabs ) {
        // Adds the new tab

            $tabs['desc_tab'] = array(
                'title'     => __( 'Technische informatie', 'woocommerce' ),
                'priority'  => 50,
                'callback'  => 'woo_new_product_tab_content'
            );
        return $tabs;   
        }
        
function woo_new_product_tab_content()  {
            // The new tab content

            $prod_id = get_the_ID();
            echo'<a class="shutter" title="technische gegevens" href="'.get_post_meta($prod_id,'_product_tech',true).'"><img width="750" height="100%" src="'.get_post_meta($prod_id,'_product_tech',true).'"></a>';
        
        }
Share Improve this question asked Jul 7, 2021 at 12:23 Coen van tartwijkCoen van tartwijk 1
Add a comment  | 

1 Answer 1

Reset to default 0

You can use empty, to check your variable.

Something like this

$prod_id = get_the_ID();
if (!empty(get_post_meta($prod_id,'_product_tech',true))) {
  echo'<a class="shutter" title="technische gegevens" href="'.get_post_meta($prod_id,'_product_tech',true).'"><img width="750" height="100%" src="'.get_post_meta($prod_id,'_product_tech',true).'"></a>';
} else {
   //Is empty
}

本文标签: phpif getpostmeta is empty echo a placeholder or shortcode