
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?