首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php函数中的if then语句

php函数中的if then语句
EN

Stack Overflow用户
提问于 2017-06-26 14:30:00
回答 2查看 65关注 0票数 1

我是一个PHP初学者。我很难在函数上做一个基本的if/then语句。

我添加了一个woocommerce产品标签'Food_paring',我想在‘food_pairing’字段为空/未设置时禁用该标签。

原始代码:

代码语言:javascript
复制
add_filter( 'woocommerce_product_tabs', 'new_product_tab' );

function new_product_tab( $tabs ) {
    /* Adds the new tab */
    $tabs['test_tab'] = array(
        'title'     => __( 'Food Pairing', 'woocommerce' ),
        'priority'  => 50,  
        'callback'  => 'food_pairing_tab_content'
    );
    return $tabs;  /* Return all  tabs including the new New Custom Product Tab  to display */
}

function food_pairing_tab_content() {
    /* The new tab content */
    echo '<h2>Food Pairing</h2><p id="tab-food-pairing">', get_post_meta( get_the_ID(), 'food_pairing', true ), '</p>';
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-26 14:42:28

请检查下面的代码。在添加选项卡之前,请检查产品元是否有值global $post; if(get_post_meta($post->ID, 'food_pairing', true )){..}

代码语言:javascript
复制
add_filter( 'woocommerce_product_tabs', 'new_product_tab' );

function new_product_tab( $tabs ) {
    global $post;
    /* Adds the new tab */
    if(get_post_meta($post->ID, 'food_pairing', true ))
    {
        $tabs['test_tab'] = array(
            'title'     => __( 'Food Pairing', 'woocommerce' ),
            'priority'  => 50,  
            'callback'  => 'food_pairing_tab_content'
        );
    }
    return $tabs;  /* Return all  tabs including the new New Custom Product Tab  to display */
}

function food_pairing_tab_content() {
    /* The new tab content */
    echo '<h2>Food Pairing</h2><p id="tab-food-pairing">', get_post_meta( get_the_ID(), 'food_pairing', true ), '</p>';
}
票数 0
EN

Stack Overflow用户

发布于 2017-06-26 15:29:21

代码语言:javascript
复制
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) { 
   global $woocommerce;
    $tabs['desc_tab'] = array( 
        'title' => __( 'Ingredients', 'woocommerce' ), 
        'priority' => 50, 
        'callback' => 'woo_new_product_tab_content' ,
     ); 
    return $tabs;
 }
function woo_new_product_tab_content() { 
        // The new tab content
          echo '<p>Lorem Ipsum</p>'; 
        echo $prod_id = get_the_ID();
        echo'<p>'.get_post_meta($prod_id,'ingredients',true).'</p>';
}

这段代码工作正常...应该试一试。祝好运

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44754116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档