ACF - get checkbox values inside flexible content field - php

Have been going around and around with this and can't make it work - probably something very simple but I've got no hair left - can anyone help me?
<?php
if( have_rows('page_content') ):
while ( have_rows('page_content') ) : the_row();
if( get_row_layout() == 'specs' ):
// check if the nested repeater field has rows of data
if( get_sub_field('objectives') ):
echo '<ul>';
$field = get_field_object('objectives');
$value = $field['value'];
$choices = $field['choices'];
// loop through the rows of data
foreach( $value as $v):
echo '<li>'.$choices.'</li>';
endforeach;
echo '</ul>';
endif;
endif;
endwhile;
endif;
?>
Thanks in advance.

Can you try with the Key of ACF field like this,
$field_key = "field_5039a99716d1d";
$field = get_field_object($field_key);
https://www.advancedcustomfields.com/resources/get_field_object/

Working now ... thanks to the great support team at ACF. I was missing the [$v] after .$choices. Working code below:
<?php
if( have_rows('page_content') ):
while ( have_rows('page_content') ) : the_row();
if( get_row_layout() == 'specs' ):
// check if the nested repeater field has rows of data
if( get_sub_field('objectives') ):
echo '<ul>';
$field = get_field_object('objectives');
$value = $field['value'];
$choices = $field['choices'];
// loop through the rows of data
foreach( $value as $v):
echo '<li>'.$choices.'</li>';
endforeach;
echo '</ul>';
endif;
endif;
endwhile;
endif;
?>

Related

Using WordPress ACF repeater in header.php

Within ACF, I have a repeater called "slider" with a radio button field. This appears on the homepage of the site.
I'd like to output the radio button field within header.php. Here's what I'vd tried:
<?php
if( have_rows('slider',$post->ID) ):
while ( have_rows('slider',$post->ID) ) : the_row();
if(get_sub_field('logo_type',$post->ID) == 'light' ) {
echo '<p>Light</p>';
}
endwhile;
endif;
?>
This is coming up empty even when I try var_dump(get_sub_field('logo_type',$post->ID));
I've also tried:
<?php
if( have_rows('slider',$post->ID) ):
global $wp_query;
$postid = $wp_query->post->ID;
while ( have_rows('slider',$postid) ) : the_row();
if(get_sub_field('logo_type',$postid) == 'light' ) {
echo '<p>Light</p>';
}
endwhile;
endif;
?>
What am I doing wrong here?
Did you try without this $post->ID
<?php
if( have_rows('slider') ):
while ( have_rows('slider') ) : the_row();
if(get_sub_field('logo_type') == 'light' ) {
echo '<p>Light</p>';
}
endwhile;
endif;
?>
I don't know if this will solve your problem but get_sub_field second parameter should not be the post id but the format value. So you would leave it empty in this case.
<?php
if( have_rows('slider',$post->ID) ):
global $wp_query;
$postid = $wp_query->post->ID;
while ( have_rows('slider',$postid) ) : the_row();
if(get_sub_field('logo_type') == 'light' ) {
echo '<p>Light</p>';
}
endwhile;
endif;
?>
I also recommend debugging what ID you're getting from $postid.
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo $postid;
?>
I think you need to add your custom field along with menu. i.e. create field group with header and assign menu is equal to your header menu name and then call that field using following code
$menu = wp_get_nav_menu_object('menuid');//replace with your menu id.
the_field('logo_type',$menu);
you can access it anywhere and you can see this field in Apperance->Menus->Menu-name->acf-field-name

list all the label and value from a group field type in ACF

I really need some help, My problem is that I cannot show the label from a group field using ACF
Script below is displaying the name and value, I need the "Label" to be displayed and its "value" and I can't find anything.
if( have_rows('product_specifications') ):
while( have_rows('product_specifications') ): the_row();
$subfields = get_field('product_specifications');
if( $subfields ) { ?>
<ul>
<?php
foreach ($subfields as $spec => $value) {
if ( !empty($value) ) { ?>
<li><?php echo $spec; ?> : <?php echo $value; ?></li>
<?php }
} ?>
</ul>
<?php }
endwhile;
endif;
Here is my current output:
lamp_type : E27
wattage : 1x 60W Max
globe_included : 1
colour_cord : Clear
when It should be:
Lamp Type : E27
Wattage : 1x 60W Max
Globe : 1
Colour Cord : Clear
Please anyone help me...
Use get_row() to get the sub fields:
$subfields = get_row();
And use get_sub_field_object() to get the sub field object:
$field = get_sub_field_object( $key );
So, try this: (no re-indentation so that you can easily compare with your code)
if( have_rows('product_specifications') ):
while( have_rows('product_specifications') ): the_row();
if( $subfields = get_row() ) { ?>
<ul>
<?php
foreach ($subfields as $key => $value) {
if ( !empty($value) ) { $field = get_sub_field_object( $key ); ?>
<li><?php echo $field['label']; ?> : <?php echo $value; ?></li>
<?php }
} ?>
</ul>
<?php }
endwhile;
endif;
What you are looking forward within the foreach loop is using the get_field_object() function.
Within here, you can get the label and value of any field.
For examples / uses of the get_field_object(), take a look at https://www.advancedcustomfields.com/resources/get_field_object/.
So, for example, you'll have:
$field = get_field_object($spec);
echo $field['label'] . ': ' . $field['value'];
Hope this helps.

Changing Repeater class based on count in Advanced Custom Fields

I need to be able to have different classes for repeater items based on the count. In other words, the first repeater item needs class="single-item active" and all other repeaters need class="single-item not-visible move-right".
This is what I have so far:
<?php $count = 0; ?>
<?php if (have_rows('features')): while (have_rows('features')) : the_row(); ?>
<?php if(!$count): ?>
<li class="cd-single-item cd-active"></li>
<?php else: ?>
<li class="cd-single-item cd-not-visible cd-move-right"></li>
<?php $count++; endif; endwhile; endif; ?>
Simply set a boolean to false after the first check:
$repeater = get_field('my_repeater_field');
$first = true;
foreach ($repeater as $sub) {
$class = $first ? 'single-item active' : 'single-item not-visible move-right';
// do whatever with $sub and $class here...
$first = false;
}
Your code is almost correct, but you're only increasing $count if (!$count) is false and this never happens because you're only increasing $count if… and so on.
Just put your $count++ after the first endif. I rewrote it like this:
<?php
$count = 0;
if ( have_rows('features') ):
while ( have_rows('features') ) : the_row();
if ( ! $count ): ?>
<li class="cd-single-item cd-active"></li>
<?php else: ?>
<li class="cd-single-item cd-not-visible cd-move-right"></li>
<?php
endif;
$count++;
endwhile;
endif;
?>
Hope this helps!

ACF loop Repeater values with get_field

I created a Custom Field with the Repeater layout to add some input text.
I would like to display all the values.
I found some code on ACF documentation but I can't understand how it works
<?php
$rows = get_field('repeater_field_name');
if($rows)
{
echo '<ul>';
foreach($rows as $row)
{
echo '<li>sub_field_1 = ' . $row['sub_field_1'] . ', sub_field_2 = ' . $row['sub_field_2'] .', etc</li>';
}
echo '</ul>';
}
?>
http://www.advancedcustomfields.com/resources/repeater/
I don't know how much fields I will create with the Repeater and I would like to loop all the values with foreach. Is that possible?
Thank you in advance
Foreach version:
<?php
$rows = get_field('repeater');
if($rows)
{
echo '<ul>';
foreach($rows as $row)
{
echo '<li>sub_field_1 = ' . $row['text'] . '</li>';
}
echo '</ul>';
}
While version:
<?php
// check if the repeater field has rows of data
if( have_rows('repeater') ):
// loop through the rows of data
while ( have_rows('repeater') ) : the_row();
// display a sub field value
the_sub_field('text');
endwhile;
else :
echo 'nothing found';
endif;
?>
I would fix it like this:
<?php
if( have_rows('slide') ):
$l= 1;
while( have_rows('slide') ): the_row();
$l++;
endwhile;
endif;
?>

Combining Multiple if into one?

I have two wordpress queries and am curious if there is a way to combine them into one?
I have tried else if, but to no avail.
<?php
$posts = get_field('appeal_forms', 'options');
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
And here the second:
<?php
$posts = get_field('misc', 'options');
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
You need to combine both arrays of posts into a single array then loop through it as you were doing.
In the example below I'm getting both arrays, checking they are valid using is_array and then merging them into one using array_merge.
<?php
// Get both post arrays.
$appeal_forms = get_field('appeal_forms', 'options');
$misc = get_field('misc', 'options');
// Check both are arrays.
if ( is_array( $appeal_forms ) && is_array( $misc ) ) {
// Combine the posts into a single array.
$posts = array_merge( $appeal_forms, $misc ); ?>
<ul>
<?php foreach ( $posts as $post ) {
setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php } ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php } ?>
If both arrays won't always have posts then the code needs to be adapted. The check I perform means that if appeal_forms or misc is empty then nothing is displayed.
If that's the case the solution I'd propose would be:
$posts = array();
// Check each array individually and add their contents to the post array.
if ( is_array( $appeal_forms ) ) {
$posts = array_merge( $posts, $appeal_forms );
}
if ( is_array( $misc ) ) {
$posts = array_merge( $posts, $misc );
}
if ( $posts ) {…

Categories