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;
?>
Related
I got a ACF Reaper field with a couple of rows I am trying to show. However, I only want the show row if it has a checkbox checked (Checkbox is a subfield within the repeater). I am trying to accomplish that by using if in_array as described in ACF documentation under "Conditional logic":
if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) )
I am outputting the result in a WordPress shortcode. For now, my code kinda works, except it shows all results within the repeater field (also those that are unchecked). What am I missing ??
My code:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
echo '<ul class="slides">';
foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) ) {
$image = $row['upload_dokument'];
echo '<li>';
echo get_field( 'upload_dokument' );
echo '</li>';
}
}
echo '</ul>';
}
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
Managed to solve issue and with the help from #maggiathor's answer. For some reason echo was causing issue. I had to use return insted:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
$content = '<ul class="dokumenter">';
foreach( $rows as $row ) {
if( !in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
$pdf = $row['upload_dokument'];
$content = $content . '<li>' . $pdf . '</li>';
}
}
}
$content = $content . '</ul>';
return $content;
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
You cannot use get_sub_field() within the foreach loop, either you need to use a have_rows-while-loop or access it from the associative array:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
echo '<ul class="slides">';
foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
$image = $row['upload_dokument'];
echo '<li>';
echo $row['upload_dokument'];
echo '</li>';
}
}
echo '</ul>';
}
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
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.
I'm using ACF pro 5. I create a Repeater Field with two sub_field, called sub_field_item & sub_field_value. I need a code which will hide if sub_field_value is empty. Probably sub_field_item contains text but if sub_field_value empty, then it will hide both.
I tried with this code, but it's not working.
<?php
if( have_rows('myrepeater') )
{
$field_key = "field_5aa18d1bc322c"; //KEY for Repeater main field "myrepeater
$field = get_field_object($field_key);
foreach($field['value'] as $value)
{
if(!empty($value['sub_field_item']))
{
$not_empty = true;
break;
}
}
if($not_empty == true)
{
echo '<h2>' . $field['label'] . '</h2>';
}
while ( have_rows('sub_field_item') )
{
the_row();
$subfield = get_sub_field('sub_field_value');
if( !empty($subfield) )
{
echo '<b>' . $subfield . '</b>';
}
}
}
?>
here is the magic code :
<?php
if(get_field('field_name')):
while(has_sub_field('field_name')):
if(get_sub_field('subfield_name')): ?>
<h2><?php echo get_sub_field('subfield_name'); ?></h2>
<?php endif;
endwhile;
endif;
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;
?>
On a wordress site I'm building, for the first time, I'm trying to add custom loops.
<?php
$custom_loop = new WP_Query('showposts=5&category_name=Featured&orderby=rand');
if ( $custom_loop->have_posts() ) :
echo '<ul>';
while ( $custom_loop->have_posts() ):
$custom_loop->the_post();
echo '<li>' . get_the_title() . '
</li>';
endwhile;
wp_reset_query();
echo '</ul>';
endif;
?>
How do I correctly add this line if ( has_post_thumbnail() ) {
the_post_thumbnail();} inbetween the <li></li> tags?
I've tried just placing it in, but it shows up like normal text on the site.
If you want to put the_post_thumbnail() value between li tag, use below code instead of your echo '<li>.....';
echo '<li>' . get_the_title() . '';
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
echo '</li>';
Try this:
<?php
$custom_loop = new WP_Query('showposts=5&category_name=Featured&orderby=rand');
if ($custom_loop->have_posts()) {
echo '<ul>';
while ($custom_loop->have_posts()) {
$custom_loop->the_post();
$thumb = (has_post_thumbnail()) ? the_post_thumbnail() : '';
printf('<li>%s%s</li>', get_permalink(), get_the_title(), $thumb);
}
wp_reset_query();
echo '</ul>';
}
?>