Wordpress (ACF) function does not return a value

2019-08-18 00:11发布

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?

2条回答
冷血范
2楼-- · 2019-08-18 00:22

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.

查看更多
相关推荐>>
3楼-- · 2019-08-18 00:29

You should be using get_field() with echo, since the_field() already echoes the meta field:

echo "<h3>" . get_field('pre_video_set_label_name') . "</h3>";

get_field() returns the meta value, rather than echoing it.

查看更多
登录 后发表回答