【问题标题】:php the best way to return empty valuephp返回空值的最佳方法
【发布时间】:2019-05-10 14:34:41
【问题描述】:

我正在使用 WordPress ACF 插件 填充一个 WordPress HTML 表格和这个功能:(我认为这是一个一般 PHP 问题

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;
}

问题是不会隐藏空单元格,因为表格单元格有一个默认值,即我的简码,我尝试使用:

return NULL;

并且也简单地使用“return”,但它们不会在表格单元格中返回空。注意 css 单独测试并且没有问题,我怎样才能返回真正的 empty 价值?

【问题讨论】:

  • 返回一个空数组怎么样,比如return array()return [] ping 我,如果它可以进入一个完整的答案。
  • 如果返回“true”空值,“默认值”如何在表格单元格中结束?
  • @DimitriosDesyllas 谢谢你,但它不起作用,因为无法处理数组并在表格单元格中打印“array()”字符串
  • 不使用else块怎么样?那有什么作用?
  • @Manjunath 没有区别也不起作用

标签: php wordpress html-table advanced-custom-fields


【解决方案1】:
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;
  }
}

【讨论】:

    【解决方案2】:

    我猜你需要检查返回值,在使用方法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?
    }
    

    【讨论】:

    • 它不起作用,因为短代码已经在表格单元格中,所以如果您不使用 add_shortcode,单元格将不会为空
    • 短代码在 html 表格文件中,例如 [custom_acf]
    【解决方案3】:

    搜索了几天后,突然看到空单元格中有一个不需要的<br>,我不知道为什么。
    之后我测试了这段代码以防止 <br> 在表格单元格中,现在的代码是这样的,但不完全是这样,并且效果很好:

    $my_empty = "";
    $my_nobr = strip_tags($my_empty, '<p><a>');
    return $my_nobr;
    

    我猜原因是在 WordPress 函数中,但我没有深入,也没有必要,这段代码解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2022-08-02
      • 1970-01-01
      • 2013-05-12
      • 2013-08-16
      • 2020-09-03
      • 1970-01-01
      • 2014-04-02
      • 2012-02-26
      • 1970-01-01
      相关资源
      最近更新 更多