I wondered if someone could help me with something that is probably quite simple when you know how.
I’m creating a game based website and I’m just working on the directory page where a list of all the games will be shown. I’m using a plug-in called ‘Advanced Custom Fields‘ to create all my custom fields for each game.
At the moment I’m running a loop to pull in each game :-
<?php $loop = new WP_Query(array('post_type' => 'games', 'posts_per_page' => 10)); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
And then within this loop calling for different custom fields of the game (title, screenshots, description, download buttons etc…). But I have a problem when it comes to the screenshots, I can happily grab all the screenshots, but I just want to pull out 4. I’ve searched around all the threads, looked through the documentation, hit google, but whatever method I’ve tried I haven’t achieved any success yet 🙁
My current code within the above loop for pulling out the screenshots is :-
<?php if(get_field('screenshots')): ?>
<ul class="screenshots">
<?php while (the_repeater_field('screenshots')): ?>
<li><a href="<?php the_sub_field('large_screenshot'); ?>" data-fancybox-group="button" class="fancybox"><img src="<?php the_sub_field('thumbnail_screenshot'); ?>" title="<?php the_sub_field('screenshot_title'); ?>"/></a></li>
<?php endwhile; ?>
</ul> <!--- end of screenshots !-->
<?php endif; ?>
Is there a way to just limit my results to a total of 4 instead of displaying all available screenshots? A random 4 would be an even nicer option, but isn’t paramount.
I’m sure the solution is far simpler than some of the things I’ve tried without success so far.
Many thanks for any help received.
Cheers
Rob
I don’t know much about the plugin you are using. but a simple solution would be using a counter like this:
I am curious as to how you got more than 1 loop to run, by definition it should work like you’re experience but for some reason when I post the same concept I can only get the while to produce one result even though there are 10 images.
I wonder if it would work if you modified your code like with && $i <= 4
Look: