Home › Forums › Add-ons › Repeater Field › Get first row of repeater
Hi all!
I bought the whole package yesterday and i’m very glad with it!
Just a minor issue: it seems that i cant get the repeater to only spit the first row!
i have a page where the images are shown with text next to them (posts). above the posts there’s a small bar with the first image of the last 5 posts. Before i was using a preg replace to get the images and text split up, but now i’m using the repeater field.
when i put the code from the website here in there, it does not generate a link in the img tag (like ).
I wandered around online a bit and found some other codes, all of them not working correctly (some stay blanc, some put all links of images in 1 img source field, some give broken images and the first image of the posts below then dissapears…)
this is the code:
<div class="postbalk">
<? query_posts('cat=4'.'&showposts=5'); // latest blog story?>
<?php while (have_posts()) : the_post(); ?>
(HERE SHOULD BE THE CODE FOR GETTING THE FIRST IMAGE)
<?php endwhile; ?>
</div>
<div class="blog_div1-blog">
<?php query_posts ($query_string . '&cat=4'); ?>
<?php while ( have_posts() ) : the_post() ?>
<div class="wrap_blog"><div class="col_blog_beeld">
<?php if( have_rows('beelden') ): ?>
<?php while( have_rows('beelden') ): the_row();
// vars
$image = get_sub_field('beelden');
$caption = get_sub_field('caption');
?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
<?php endwhile; ?>
<?php endif; ?>
ANY help on this is greatly apreciated!!!
thank you!
Mathieu
you can use print_r for view repeater field array and then use first row of array
<?php print_r(get_field('beelden')); ?>
for example:
<?php
$repeater = get_field(‘beelden’);
$first_img = $repeater[0][‘img’];
?>
Hi navid66
when the example u have given, if i do <img src="<?php echo $first_img ?>">
that again results in
<img src="">
anything i’m doing wrong?
regards!
Mathieu
@mathieu Is possible to give output print_r(get_field('beelden'));
?
Hey navid66
this is what i got for output:
Array ( [0] => Array ( [beelden] => Array ( [ID] => 110 [id] => 110 [alt] => [title] => (C)Carine Vangerven_DSC_1795_076 [caption] => [description] => [url] => http://localhost:8888/aod/wp-content/uploads/2014/06/CCarine-Vangerven_DSC_1795_076.jpg [width] => 2667 [height] => 4000 [sizes] => Array ( [thumbnail] => http://localhost:8888/aod/wp-content/uploads/2014/06/CCarine-Vangerven_DSC_1795_076-150x150.jpg [thumbnail-width] => 150 [thumbnail-height] => 150 [medium] => http://localhost:8888/aod/wp-content/uploads/2014/06/CCarine-Vangerven_DSC_1795_076-200x300.jpg [medium-width] => 200 [medium-height] => 300 [large] => http://localhost:8888/aod/wp-content/uploads/2014/06/CCarine-Vangerven_DSC_1795_076-682x1024.jpg [large-width] => 682 [large-height] => 1024 ) ) [caption] => ) ) Array ( [0] => Array ( [beelden] => Array ( [ID] => 65 [id] => 65 [alt] => [title] => Schermafbeelding 2014-06-29 om 13.10.11 [caption] => [description] => [url] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-29-om-13.10.11.png [width] => 773 [height] => 513 [sizes] => Array ( [thumbnail] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-29-om-13.10.11-150x150.png [thumbnail-width] => 150 [thumbnail-height] => 150 [medium] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-29-om-13.10.11-300x199.png [medium-width] => 300 [medium-height] => 199 [large] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-29-om-13.10.11.png [large-width] => 773 [large-height] => 513 ) ) [caption] => ) [1] => Array ( [beelden] => Array ( [ID] => 66 [id] => 66 [alt] => [title] => Schermafbeelding 2014-06-29 om 13.09.46 [caption] => [description] => [url] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-29-om-13.09.46.png [width] => 771 [height] => 512 [sizes] => Array ( [thumbnail] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-29-om-13.09.46-150x150.png [thumbnail-width] => 150 [thumbnail-height] => 150 [medium] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-29-om-13.09.46-300x199.png [medium-width] => 300 [medium-height] => 199 [large] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-29-om-13.09.46.png [large-width] => 771 [large-height] => 512 ) ) [caption] => ) [2] => Array ( [beelden] => Array ( [ID] => 64 [id] => 64 [alt] => [title] => Schermafbeelding 2014-06-29 om 13.10.39 [caption] => [description] => [url] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-29-om-13.10.39.png [width] => 771 [height] => 509 [sizes] => Array ( [thumbnail] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-29-om-13.10.39-150x150.png [thumbnail-width] => 150 [thumbnail-height] => 150 [medium] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-29-om-13.10.39-300x198.png [medium-width] => 300 [medium-height] => 198 [large] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-29-om-13.10.39.png [large-width] => 771 [large-height] => 509 ) ) [caption] => ) ) Array ( [0] => Array ( [beelden] => Array ( [ID] => 56 [id] => 56 [alt] => [title] => Schermafbeelding 2014-06-22 om 18.20.00 [caption] => [description] => [url] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-22-om-18.20.00.png [width] => 856 [height] => 682 [sizes] => Array ( [thumbnail] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-22-om-18.20.00-150x150.png [thumbnail-width] => 150 [thumbnail-height] => 150 [medium] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-22-om-18.20.00-300x239.png [medium-width] => 300 [medium-height] => 239 [large] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-22-om-18.20.00.png [large-width] => 856 [large-height] => 682 ) ) [caption] => ) [1] => Array ( [beelden] => Array ( [ID] => 57 [id] => 57 [alt] => [title] => Schermafbeelding 2014-06-17 om 10.36.21 [caption] => [description] => [url] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-17-om-10.36.21.png [width] => 1430 [height] => 895 [sizes] => Array ( [thumbnail] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-17-om-10.36.21-150x150.png [thumbnail-width] => 150 [thumbnail-height] => 150 [medium] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-17-om-10.36.21-300x187.png [medium-width] => 300 [medium-height] => 187 [large] => http://localhost:8888/aod/wp-content/uploads/2014/06/Schermafbeelding-2014-06-17-om-10.36.21-1024x640.png [large-width] => 1024 [large-height] => 640 ) ) [caption] => ) )
tnx @mathieu
try this :
$main_field = get_field('beelden');
$first_img = $main_field[0]['beelden']['url'];
The topic ‘Get first row of repeater’ 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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.