To output a WYSIWYG (What You See Is What You Get) field with Advanced Custom Fields (ACF) in a WordPress template, you need to add a snippet of PHP code to your template file where you want the field’s content to appear.
Firstly, you’ll need to create your WYSIWYG field using ACF. In the ACF settings, you should give your field a name (field_name, for instance).
Then, to display the contents of this field in your WordPress template, you’d typically use the get_field()
function that ACF provides. This function will fetch the data from your custom field.
Here is a basic example:
<?php
$field_name = get_field('field_name');
if($field_name) {
echo $field_name;
}
?>
In this example, replace 'field_name'
with the actual name of your WYSIWYG field. This script first tries to fetch the field’s data. If it exists ($field_name
is truthy), the data is output to the page using the echo
statement.
This will automatically output your WYSIWYG field data in the right place in your WordPress template. However, please note that it’s important to sanitize and validate any data that you output in this way, to protect against potential security issues.
Since ACF automatically applies wpautop
(to convert line breaks into <p> and
<br> elements) on all WYSIWYG fields, the output should already have the necessary HTML formatting.
Also, make sure that the WYSIWYG field is set up to return a value (and not an array or an ID) in the ACF field settings. If it’s set up to return an array or an ID, you’ll need to use a different method to fetch and display the field’s content.