ACF repeater showing just 1 row - php

So when I try to use ACF repeater field instead of showing me all the fields I get just the first one. The code is as follows.
<?php if( have_rows('vsi_projekti') ): ?>
<ul class="posts-grid">
<?php while( have_rows('vsi_projekti') ): the_row();
// vars
$image = get_sub_field('vsi_projekti_image');
$content = get_sub_field('project_name');
$link = get_sub_field('link_to_post');
?>
<li class="post-grid">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
<div class="post-title-hover"><?php echo $content; ?></div>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Any advice on what I'm doing wrong to be getting out just 1 row instead of multiple?
I don't know if this has anything to do with my problem or not, but I'm adding just 1 row in every post. But in the end I should get out more then just row I think?

I think that you are confused what a ACF repeater field does. If you enter just one row in every post witrh a repeater, it's normal that you get only one row... because your code works perfectly fine ... for a repeater within a post... when you add 15 rowds in your post you will get all 15rows as output...
But if you want to output every row of every repeater of every post, yopur code doesn't work. You should try this instead:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1
);
$posts = get_posts($args);
if( $posts ): ?>
<ul class="posts-grid">
<?php foreach( $posts as $post ): setup_postdata( $post ); ?>
<?php if( have_rows('vsi_projekti') ): ?>
<?php while( have_rows('vsi_projekti') ): the_row();
// vars
$image = get_sub_field('vsi_projekti_image');
$content = get_sub_field('project_name');
$link = get_sub_field('link_to_post');
?>
<li class="post-grid">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
<div class="post-title-hover"><?php echo $content; ?></div>
</a>
</li>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; //foreach( $posts as $post ) ?>
<?php wp_reset_postdata(); ?>
</ul>
<?php endif; // if( $posts ) ?>
This code gets all posts and loops throug them... In every loop the repeater field is put out....

Related

trying to insert php into img src attribute to display featured image on custom post archive page in Wordpress

The image is set to return a URL in the Custom Fields Plugin however it is just coming up with img src Unknown, everything else works on the page except this... code is as follows: Thanks in advance for any help!
<?php get_header(); ?>
<?php
$args = array(
'post_type' => 'artists',
);
$query = new WP_Query( $args );
?>
<section class="row artists">
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col-sm-12 col-md-6 col-lg-3">
<a href="<?php if( get_post_field('artist_website') ) { ?>
<?php echo $artist_website; ?>
<?php } else { the_permalink(); } ?>">
<img src="<?php get_post_field('artist_feature_image'); ?>" alt="<?php echo the_title() ; ?>">
<p><?php echo the_title() ;?></p>
</a>
</div>
<?php endwhile; endif; wp_reset_postdata(); ?>
</section>
<?php get_footer(); ?>
The problem is that get_post_field('artist_feature_image') only return the image.
You need to display it with "echo"
<img src="<?php echo get_post_field('artist_feature_image'); ?>" />
And don't do echo the_title() ;, because the_title() is already doing an echo on get_the_title();
function the_title() {
echo get_the_title();
}
So if you want to display it you just have to do: the_title();

Advance Custom fields keep returning the same value

So to elaborate, I have 11 posts and in each of those posts I input an image into advance custom fields form. But when I call them I get back 11 results but from just 1 post.
Here is what I'm working with. Just to tell you this is in functiuons.php since I want to get this as a shortcode so I can use it on multiple post types.
function get_slider() {
$args = array(
'post_type' => 'projekti',
'posts_per_page' => -1,
);
$posts = get_posts($args);
ob_start();
if( $posts ): ?>
<div class="slider_slick">
<?php foreach($posts as $post): setup_postdata( $post ); ?>
<?php if( have_rows('slider') ): ?>
<?php while( have_rows('slider') ): the_row(); ?>
<?php // vars
$image = get_sub_field('image_slider');
$link = get_sub_field('slider_link');
?>
<div class="slide">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</div>
<?php endif;
return ob_get_clean();
}
add_shortcode ('slick_slider' , 'get_slider');
I have almost identical code on the template I created, but that one works, this one does not and I don't know why.
Pass id of current post for the acf have_rows() function. I have done that using , $currentId = get_the_ID(); and then using the variable $currentId where I need.
function get_slider() {
$args = array(
'post_type' => 'projekti',
'posts_per_page' => -1,
);
$posts = get_posts($args);
ob_start();
if( $posts ): ?>
<div class="slider_slick">
<?php foreach($posts as $post): setup_postdata( $post );
$currentId = get_the_ID();
?>
<?php if( have_rows('slider', $currentId) ): ?>
<?php while( have_rows('slider', $currentId) ): the_row(); ?>
<?php // vars
$image = get_sub_field('image_slider');
$link = get_sub_field('slider_link');
?>
<div class="slide">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</div>
<?php endif;
return ob_get_clean();
}
add_shortcode ('slick_slider' , 'get_slider');
I had to add
global $post;
to the top of my function for it to work. Thanks for your help anyway guys.

How do I append text in ACF for field type repeater?

Sometime I will have 2 links or 1 link.
<a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>
I want to add "or" between the 2 links. So if there are 2 links, add "or"
For example, "Soundcloud or Youtube"
If there is 1 links don't add "or". For example, "Soundcloud"
<div class="container-wrap" id="music">
<?php
$args = array('post_type' => 'music-playlist-1');
$query = new WP_Query($args);
$cntr = 0;
while( $query -> have_posts() ) : $query -> the_post(); $cntr++; ?>
<section class="row-wrap">
<div class="row-inner">
<?php if ($cntr % 2 == 1) { ?>
<?php
$image = get_field('artwork');
if( !empty($image) ): ?>
<img class="artwork" src="<?php echo $image['url']; ?>">
<?php endif; ?>
<div class="artwork-content">
<h1><?php the_field('playlist-name'); ?></h1>
<button class="btn-wrap">
<div class="btn">listen now</div>
</button>
<div class="option-bar">
<?php
// check if the repeater field has rows of data
if( have_rows('playlist_link') ):
// loop through the rows of data
while ( have_rows('playlist_link') ) : the_row();
?>
<a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>
<?php
endwhile;
else :
// no rows found
endif;
?>
</div>
</div>
<?php } else { ?>
<div class="artwork-content">
<h1><?php the_field('playlist-name'); ?></h1>
<button class="btn-wrap">
<div class="btn">listen now</div>
</button>
<div class="option-bar">
<?php
// check if the repeater field has rows of data
if( have_rows('playlist_link') ):
// loop through the rows of data
while ( have_rows('playlist_link') ) : the_row();
?>
<a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>
<?php
endwhile;
else :
// no rows found
endif;
?>
</div>
</div>
<?php
$image = get_field('artwork');
if( !empty($image) ): ?>
<img class="artwork" src="<?php echo $image['url']; ?>">
<?php endif; ?>
<?php } ?>
</div>
</section>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
I've never actually used the repeater rows, but looking at the docs I'll assume you can check if you are on first row if get_row_index() is 0 (since there seems to be no way to check if you are on the last row). Thus:
edit: ok now I HAVE tried it out. sorry. get_row_index() is new in version 5.3.4. Maybe that's your problem?
Here is a solution without get_row_index()
I added + before each line I added.
<?php
// check if the repeater field has rows of data
if (have_rows('playlist_link')):
+ // We set a flag for the first row
+ $is_first = true;
// loop through the rows of data
while (have_rows('playlist_link')) : the_row();
+ if ($is_first) :
+ $is_first = false;
+ else :
+ echo " OR ";
+ endif; ?>
<a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>
<?php
endwhile;
else :
// no rows found
endif;
?>
Edit 2: How do we make this reusable?
Ok, I'll do this the super simple way. I take all the code, wrap it in a function... and put it in functions.php:
function playlist_links() { // <--This line is new
if (have_rows('playlist_link')):
$is_first = true;
while (have_rows('playlist_link')) : the_row();
if ($is_first) :
$is_first = false;
else :
echo " OR ";
endif; ?>
<a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>
<?php
endwhile;
endif;
} // <--This line is also new
Then in your template-file, you can replace all that code with
<?php playlist_links(); ?>
And use it several times if you so wish.
(If you only use this function on a single page template, then maybe functions.php is not the greatest place for it since it get's loaded everywhere. You can instead make functions directly in your template-file.)

ACF Repeatable Content Widget - WordPress

I've created a Field group called 'Team Members' and set the field type as repeatable with three sub fields; Image, Position and Name.
The rule for this group is to show within a custom widget I've created. When I drag the widget in the Appearance > Widgets area of WordPress I can see it is indeed there and can add members / images to my widget.
Where I've hit a brick wall is trying to output this data on the front end through my widget template.
This is my code:
<?php if( have_rows('team_members') ): ?>
<ul class="slides">
<?php while( have_rows('team_members') ): the_row();
// vars
$image = get_sub_field('image');
$content = get_sub_field('name');
$link = get_sub_field('position');
?>
<li class="slide">
<?php if( $link ): ?>
<a href="<?php echo $link; ?>">
<?php endif; ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
<?php if( $link ): ?>
</a>
<?php endif; ?>
<?php echo $content; ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Where am I going wrong?
To get values from your custom widget via ACF the syntax is:
if ( have_rows( 'team_members', 'widget_' . $widget_id ) ) :
while have_rows( 'team_members', 'widget_' . $widget_id ) ) : the_row();
// get sub fields ...
endwhile;
endif;

Post Objects in Advanced Custom Fields order by date

Hi I have a Post Object field in Advanced Custom Fields that I want to return multiple posts, ordered by date. I have the custom field data from those posts returning fine, but the Post Objects return in order of the Post ID. I want them to be ordered by the date that the post was published.
<?php $post_objects = get_field('exhibitions');
if( $post_objects ): ?>
<?php foreach( $post_objects as $post_object): ?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<div style="display: inline-block">
<? if( get_field( 'title', $post_object->ID) ): ?>
<em><?php the_field('title', $post_object->ID); ?></em><br>
<?php endif; ?>
<? if( get_field( 'dates', $post_object->ID) ): ?>
<?php the_field('dates', $post_object->ID); ?>
<?php endif; ?>
</div>
</a>
<br><br>
<?php endforeach; ?>
<?php endif; ?>
This returns the text custom fields 'title' and 'dates' from each post thats selected in the Post Objects field on the post where this is called.
I want the posts to return here by order of their publish date.
Any ideas?
#Michael Ray-Von - your answer worked, but it involved getting the same data from the db twice. Instead you can just sort the post data returned in your initial ACF query rather than running the extra query. (The post_date is returned as a string so you can strcmp it):
<?php
// get the posts from ACF
$custom_posts = get_field('your_posts_field');
// sort the posts by post date, but you can also sort on ID or whatever
usort($custom_posts, function($a, $b) {
return strcmp($b->post_date,$a->post_date);
});
// write them out
foreach ($custom_posts as $post) : setup_postdata($post); ?>
<article>
<h1><?php the_title();?></h1>
<?php the_excerpt(); ?>
</article>
<?php
endforeach;
wp_reset_query();
?>
Hat-tip to this answer for the sorting: https://stackoverflow.com/a/10159521
Okay i've got it figured out!
Instead of calling get_field as the post_objects, you call it as a variable just to get the IDs of relevant posts, and then use that in an array for the $args of a get_posts. That way you have access to all the array options of get_posts before running the loop.
<?php
$ids = get_field('exhibitions', false, false);
$args = array(
'post__in' => $ids,
'orderby' => 'post_date',
);
$post_objects = get_posts( $args );
if( $post_objects ): ?>
<?php foreach( $post_objects as $post_object): ?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<div style="display: inline-block">
<? if( get_field( 'title', $post_object->ID) ): ?>
<em><?php the_field('title', $post_object->ID); ?></em><br>
<?php endif; ?>
<? if( get_field( 'dates', $post_object->ID) ): ?>
<?php the_field('dates', $post_object->ID); ?>
<?php endif; ?>
</div>
</a>
<br><br>
<?php endforeach; ?>
<?php endif; ?>
Thanks for your help!
found my answer thanks to: http://support.advancedcustomfields.com/discussion/5846/adding-args-to-post_objects-get_field/p1
<?php $post_objects = get_field('exhibitions');
$args = array (
'orderby'=>'date',
);
$the_query = new WP_Query( $args );
if($the_query->have_posts()) {
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<div style="display: inline-block">
<? if( get_field( 'title', $post_object->ID) ): ?>
<em><?php the_field('title', $post_object->ID); ?></em><br>
<?php endif; ?>
<? if( get_field( 'dates', $post_object->ID) ): ?>
<?php the_field('dates', $post_object->ID); ?>
<?php endif; ?>` </div>
</a>
<br><br>
endwhile;
wp_reset_postdata();
}
I haven't tested, But it should work for you with little adaption !

Categories