首页 文章

php返回空值的最佳方法

提问于
浏览
0

我正在使用WordPress ACF plugin 和这个函数填充 WordPress HTML表:(我认为它是 general PHP question

add_shortcode('custom_acf',custom_acf_callback);
function custom_acf_callback ($atts = '')
  {
  $value = shortcode_atts( array(
    'my_field' => '',
   ), $atts );
 //the main part starts from here:
   if ($custom_acf_value){

        return $custom_acf_value;
    }
    else {
//and the problem is here:
        return '';
    }

我使用这个棘手的CSS代码隐藏空表单元格:

td:empty {
display: none;
}

Problem 是空单元格不会被隐藏,因为表格单元格的默认值是我的短代码,我尝试使用:

return NULL;

并且还简单地使用"return",但它们不会在表格单元格中返回空 . 请注意 css 是单独测试的并且没有问题,如何返回真正的 empty 值?

2 回答

  • 0

    我想你需要在使用方法 add_shortcode 之前检查返回值:

    $value = custom_acf_callback();
    
    if($value){
        add_shortcode('custom_acf', $value);
    } else {
        add_shortcode(); // not sure what this method does and if you need it to generate empty cells?
    }
    
  • 0
    add_shortcode('custom_acf',custom_acf_callback);
    
    function custom_acf_callback ($atts = '')
    {
      $value = shortcode_atts( array(
        'my_field' => '',
      ), $atts );
    
      // the main part starts from here:
      if ($custom_acf_value) {
        return $custom_acf_value;
      }
    }
    

相关问题