Support

Account

Home Forums General Issues Multiple ACF fields before excerpt

Solved

Multiple ACF fields before excerpt

  • Hollers!

    I’m trying to return two ACF fields before WordPress excerpt and plain text between these two ACF fields. I’ve managed to pull out one ACF field before excerpt with following code, but this is where my googling skills ended:

    add_filter(‘the_excerpt’, ‘my_function_name’);

    function my_function_name($excerpt) {
    $my_acf_field = the_field(‘my_acf_field’);
    return $my_acf_field . ‘lorem ipsum’ . $excerpt;
    }

    How to get another ACF field after ‘lorem ipsum’? I have no php skills whatsoever, but by trial and error I managed to return two ACF fields by doing this:

    add_filter(‘the_excerpt’, ‘my_function_name’);

    function my_function_name($excerpt) {
    $my_acf_field1 = the_field(‘my_acf_field1’);
    $my_acf_field2 = the_field(‘my_acf_field2’);
    return $my_acf_field1 . ‘lorem ipsum’ . my_acf_field2 . $excerpt;
    }

    But this resulted “my_acf_field1 + my_acf_field2 + lorem ipsum + excerpt”, so I just couldn’t get the “lorem ipsum” between the two ACF fields no matter what I tried and at the same time I really don’t know if this is the right function to call two ACF fields anyway. Any help?

    Big thanks in advance!

  • Hi Yormario, I believe you’re almost there! It looks like you are using the wrong field function for your situation.

    The solution would be to use get_field() which will allow you to get the input and store it in a variable to return.

    If you want a more in depth explanation, the reason your ACF Fields are not showing in the right order is because the_field() doesn’t return a value, but echos it. It is essentially the same as doing echo get_field();

    This means the values aren’t stored and instead are echoed in the order they are called, before the filter function is returned.

    More information here: https://www.advancedcustomfields.com/resources/get_field/

    Also, small, but important difference you are missing the $ for the second field variable in the return statement.

  • This did the trick indeed!

    add_filter(‘the_excerpt’, ‘my_function_name’);

    function my_function_name($excerpt) {
    $my_acf_field1 = get_field(‘my_acf_field1’);
    $my_acf_field2 = get_field(‘my_acf_field2’);
    return $my_acf_field1 . ‘lorem ipsum’ . $my_acf_field2 . $excerpt;
    }

    BIG thanks jared-rice!
    BR/Yormario

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.