Home › Forums › Front-end Issues › Radio field conditional in afc_form?
Hi
I have create a post frontend with acf_form. How to select conditional radio field?
This is my code, but not correct. I using code if(get_field('chon') == 'a') {}
Field name radio: chon
Choice: a : Image
b : Video
createpost.php
<?php acf_form_head(); ?>
<?php
/*
Template Name: dangbai
*/
get_header();
?>
<div id="primary" class="content-area home">
<main id="main" class="site-main" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php
acf_form(array(
'post_id' => 'new_post',
'submit_value' => 'Đăng bài',
'field_groups' => array( 305 ),
'updated_message' => 'Đã đăng bài'
));
?>
<?php endwhile; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Function.php
function my_pre_save_post( $post_id ) {
// bail early if not a new post
if( $post_id !== 'new_post' ) {
return $post_id;
}
// vars
//$title = $_POST['fields']['field_5558a08e10eed'];
//$content = $_POST['fields']['field_555ddb4112814'];
//If
if(get_field('chon') == 'a') {
$title = $_POST['fields']['field_5558a08e10eed'];
$content = $_POST['fields']['field_555ddb4112814'];
}
else if (get_field('chon') == 'b') {
$title = $_POST['fields']['field_555efea0f3e01'];
$content = $_POST['fields']['field_555efed7f3e02'];
}
// Create a new post
$post = array(
'post_status' => 'publish',
'post_type' => 'post',
'post_title' => $title,
'post_content' => $content
);
// insert the post
$post_id = wp_insert_post( $post );
// update $_POST['return']
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
Thanks!
get_field(‘chon’) is not going to work here.
if(get_field('chon') == 'a') {
$title = $_POST['fields']['field_5558a08e10eed'];
$content = $_POST['fields']['field_555ddb4112814'];
}
else if (get_field('chon') == 'b') {
$title = $_POST['fields']['field_555efea0f3e01'];
$content = $_POST['fields']['field_555efed7f3e02'];
}
Your function is called before the field is saved, so it will have no value.
Your accessing the value for the title and content the correct way, you” need to access the value of ‘chon’ the same way.
The topic ‘Radio field conditional in afc_form?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.