【问题标题】:PHP if statement, variable not displayingPHP if 语句,变量不显示
【发布时间】:2012-04-30 23:05:19
【问题描述】:

我有以下代码:

<?php if(the_field('required_libraries') ) { echo 'Title' . $required_libraries; } ?>

该字段确实存在并且正确显示。但是,“标题”文本没有。

这适用于没有任何变量的我,所以我不太明白为什么它在这里不起作用。

【问题讨论】:

  • 只要if 语句正在执行,它应该可以正常工作
  • the_field 返回什么?如果它确实返回了一些东西,那么它作为布尔值的解释是什么?
  • @Nicolás PHP 中没有什么是假的
  • @RepWhoringPeeHaa 你是在告诉我0 等等,不是假的吗?那么boolean casting 呢?
  • 我正在使用提供的代码,无论我如何更改它,它仍然不会显示。 advancedcustomfields.com/docs/code-examples

标签: php variables if-statement field echo


【解决方案1】:

我不确定我是否理解您的问题。您可能需要在 if 语句中使用 isset。

<?php

    if (isset($required_libraries)) {
        echo 'Title' . $required_libraries;
    }

?>

你能发布你的“the_field”函数做什么吗?

更新

根据您提供的文档,您应该在 if 语句中使用 get_field()(而不是 the_field())。

<?php

    if(get_field('required_libraries')) {
        echo 'Title ' . get_field('required_libraries');
    }

?>

看起来 the_field() 会回显字段值,因此您也可以这样做:

<?php

    if(get_field('required_libraries')) {
        echo 'Title ';
        the_field('required_libraries');
    }

?>

【讨论】:

  • 它检索一个自定义字段并显示它:advancedcustomfields.com/docs/code-examples
  • 我用新的例子更新了我的答案。看起来您需要在 if 语句中使用 get_field 。使用 get_field 获取字段值。使用 the_field 作为回显值的快捷方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多