Wordpress(ACF)函数不返回值 [英] Wordpress (ACF) function does not return a value

查看:231
本文介绍了Wordpress(ACF)函数不返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够通过变量解析数据没有问题,但我的回声的HTML输出没有被正确包装。

 <? 
if(get_field('pre_video_set_label_name')){
echo< h3> 。 the_field('pre_video_set_label_name')。 < / H3>;
} else {
echo< h3>事件后视频< / h3>;
}
?>

如果我对 pre_video_set_label_name 的输入是Test 然后HTML输出变成:

 测试< h3>< / h3> 

我的预期输出是:

 < H3>试验< / H3> 

但我没有得到这些结果。



似乎没有什么可以包装的,最近我一直都有这个问题。当我使用 时,我的思维方式出现了错误吗?解析方案

.wordpress.org / reference /rel =nofollow> wordpress /( ACF )函数,总是检查它们是否显示返回值
$ b

函数,显示值:



函数displayX(){
echo data;

如果你想调用这个函数,你不需要<$ em c $ c> echo 来显示数据,只需调用它,例如

  displayX(); //输出:data 

注意:函数不会返回数据。但即使它没有显式的返回语句也不会返回数据,但它仍然会返回一些东西(NULL)



函数,它返回值:



函数returnX(){
return data;




如果你想调用这个函数,你需要一个 echo 来显示数据,只需调用它,例如

  echo returnX(); //输出:数据

注意:此函数将返回数据并不会显示它自己。



不同的行为



当您使用时,您会注意到一些差异显示或返回值的函数。


  1. 赋值



    1.1函数,它显示的值:

      $ variable = displayX(); 

    注意: $ variable ,将被赋予 NULL ,而上面的行将输出 data


    $ b

    1.2函数,它返回的值:

      $ variable = returnX(); 

    注意: $ variable ,将被赋予 data ,而上面的行不会输出任何内容。


  2. 串联



    2.1函数,其中显示值:

      echostring start。 displayX()。 字符串结束; 

    注意:您将连接 NULL 这里,因为这个函数会返回这个值。在你看到连接字符串之前,函数显示 data



    2.2函数,返回值

      $ variable = returnX(); 

    注意:您将连接 data 这里,因为这个函数会返回这个值。


  3. 打印


    功能不会先显示任何内容。

    3.1函数,它显示的值:

      displayX(); 

    注意:此代码输出 data



    3.2函数,返回值:

      returnX(); 

    注意:此代码不会显示任何内容。







因此,在您当前的示例中,您使用 the_field() 显示数据。但是,如果要连接它,则需要返回的数据,表示使用 get_filed() ,它只是返回数据。



还有一种简单的方法来检查函数返回的内容。只要做: var_dump(functionCall()); ,你会看到函数返回的结果。


I am able to parse the data through the variable no problem, but the HTML output for my echo is not being wrapped properly.

<?
    if( get_field('pre_video_set_label_name') ) {
        echo "<h3>" . the_field('pre_video_set_label_name') . "</h3>";
    } else {
        echo "<h3>Post-Event Video</h3>";
    }
?>

If my input for pre_video_set_label_name is "Test" then the HTML output becomes:

Test<h3></h3>

My expected output would be:

<h3>Test</h3>

But I'm not getting these results.

Nothing seems to wrap, and I've been having this problem a lot lately. Is there an error in my way of thinking with this?

解决方案

When you use wordpress / (ACF) functions, always check if they display or return the value.

Function, which displays the value:

function displayX(){
    echo "data";
}

If you want to call this function, you won't need a echo to display the data, just call it, e.g.

displayX();  //output: data

Note: The function, won't return the data. But even if it doesn't have an explicit return statement and also won't return the data, it still will return something (NULL).

Function, which returns the value:

function returnX(){
    return "data";
}

If you want to call this function, you will need a echo to display the data, just call it, e.g.

echo returnX();  //output: data

Note: This function will return the data and doesn't display it by its own.

Different behaviour

You will notice some differences, when you use functions which display or return a value.

  1. Assignment

    1.1 Function, which displays the value:

    $variable = displayX();
    

    Note: $variable, will be assigned with NULL and the line above will output data.

    1.2 Function, which returns the value:

    $variable = returnX();
    

    Note: $variable, will be assigned with data and the line above won't output anything.

  2. Concatenation

    2.1 Function, which displays the value:

    echo "string start " . displayX() . " string end";
    

    Note: You will concatenate NULL here, since this function will return this value. The function will display data first, before you see the concatenated string.

    2.2 Function, which returns the value:

    $variable = returnX();
    

    Note: You will concatenate data here, since this function will return this value. The function won't display anything first, before you see the concatenated string.

  3. Printing

    3.1 Function, which displays the value:

    displayX();
    

    Note: This code will output data.

    3.2 Function, which returns the value:

    returnX();
    

    Note: This code won't display anything.


So in your current example you use the_field(), which displays the data. But if you want to concatenate it, you will need the data returned, means use get_filed(), which simply will return the data.

There is also a easy way to check what a function returns. Just do: var_dump(functionCall()); and you will see what the function returns.

这篇关于Wordpress(ACF)函数不返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆