Home › Forums › Add-ons › Repeater Field › True/False field returns always 1 as value in front end
Hello,
I’m creating a simple function in my wordpress to display a message via shortcode, basing on a true/false field status. I’m using a true/false field inside a repeater.
Here’s my code:
function wpb_demo_shortcode() {
if( have_rows('documentazione_organizzazione') ){
while( have_rows('documentazione_organizzazione') ) {
the_row();
// Get sub value.
$switchFileLink = get_sub_field('file_link');
$linkAttached = get_sub_field('link');
$fileAttached = get_sub_field('file');
$myInt = (int)$switchFileLink;
var_dump($myInt);
var_dump($switchFileLink);
//Choose the value
if($myInt == 1 ) {
$message = 'File!';
}
if($myInt == 0) {
$message = 'Link!';
}
}
}
// Output needs to be return
return $message;
}
// register shortcode
add_shortcode('greeting', 'wpb_demo_shortcode');
To be sure, first I have converted the boolean value of true/false field into integer ($myInt), since I know the values stored in DB are 1 or 0.
If I var_dump $myInt, the correct value is displayed but it keeps returning 1 to every repeater element on the front end. In few words, it always appear message “File” because of the returning value (always 1).
How can I solve this?
Thank you!
where is the shortcode being called? Inside or outside of “The Loop”
When used outside of the loop ACF may not be able to determine what post you’re trying to get the value for.
Hello John! Thanks for the quick reply!
I’m using it outside the loop.
I need to use the shorcode inside an Elementor widget.
Then you will need to figure out the post ID and supply it to ACF when calling have_rows('repeater_field', $post_id)
I tried but nothing happened…
$post_id= 4054;
if( have_rows('documentazione_organizzazione', $post_id) ){
while( have_rows('documentazione_organizzazione', $post_id)) {
the_row();
[...]
Any idea?
The topic ‘True/False field returns always 1 as value in front end’ 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.