How can I set the default value of a field dynamically? For example, if there’s an entry in the field than it will show that. But if there’s none then it will show the post title there.
From the thread(https://support.advancedcustomfields.com/forums/topic/dynamically-set-acf-field-default-value), I tried somethings below but it didn’t seem to work.
function my_acf_load_field( $field ) {
$address = get_the_title();
$field['default_value'] = $address;
return $field;
}
add_filter('acf/load_field/name=character_overview_title', 'my_acf_load_field');
can anyone help me how to configure this so that it reflects in the default value in front and backend?
function my_acf_load_field($value, $post_id, $field) {
if (empty($value)) {
$value = get_the_title($post_id);
}
return $value;
}
add_filter('acf/load_value/name=character_overview_title', 'my_acf_load_field', 20, 3);
if you field has never been set then this will not work, in this case you must put it in the code where you want do show it.
$text = get_field('character_overview_title');
if (emtpy($text)) {
$text = get_the_title();
}
echo $text;
<?php
$text = get_field('character_overview_title');
if (emtpy($text)) {
$text = get_the_title();
}
?>
<div class="character-overview" id="overview">
<h2><?php echo $text; ?> Overview</h2>
<p class="overview-description"><?php the_field( 'character_overview_description' ); ?></p>
</div>
Breaks the site for some reason.
typo in my code, it should be “empty” and not “emtpy”