Using add_filter to prepend content, result is out of order

I’m using advanced custom fields to add a few fields to my posts. I want to prepend one of these fields to the content. I think I can do this using filters in wp, but I’m getting an odd result.

My filter to prepend a link to the content.

function add_fields_to_content($content)
{
    $acf_library_url = the_field('acf_library_url');
    $linkDisplay = '<a href="' . $acf_library_url . '">Link</a>';
    return $linkDisplay .= $content;
}
add_filter('the_content', 'add_fields_to_content');

When the above filter runs I’d expect it to prepend a link to my content. For example


<a href="linktoexternalsite.com">Link</a>
... the rest of the content.

Instead, the url is appearing outside of the <a> element and for some reason the URL of the posts is placed inside the href attribute. I think I’m misunderstanding something about how filters work in WP…

Using add_filter to prepend content, result is out of order

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The problem not it wp hook,
the_field() displays the value of a specific field, so you need to use get_field() instead, which returns a value.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x