Hello,
I need to get the count of the fields on some particular [[LINK href="http://www.advancedcustomfields.com/"]]advanced custom fields[[/LINK]] in my post.
Also this needs to be done in the loop which hopefully makes this simpler.
Please see below my mark and what fields I need to retrieve the count of.
<?php if (have_posts()) : ?>
<div class="post">
<?php while (have_posts()) : the_post(); ?>
<!-- begin repeater field -->
<?php if ( get_field('document_downloads') ): ?>
<?php while( has_sub_field('document_downloads') ): ?>
<?php the_sub_field('document_name'); ?>
<?php endwhile; ?>
<?php endif; wp_reset_query(); ?>
<!-- end -->
<!-- begin gallery field -->
<?php $images = get_field('image_downloads'); if( $images ): ?>
<?php foreach( $images as $image ): ?>
<?php echo $image['id']; ?>
<?php endforeach; ?>
<?php endif; wp_reset_query(); ?>
<!-- end -->
<!-- begin repeater field -->
<?php if ( get_field('video_download') ): ?>
<?php while( has_sub_field('video_download') ): ?>
<?php the_sub_field('video_file'); ?>
<?php endwhile; ?>
<?php endif; wp_reset_query(); ?>
<!-- end -->
<?php endwhile; ?>
</div>
<?php endif; wp_reset_query(); ?>
As you can see above, I have two types of field count I need to retrieve.
Two are repeater fields, and one is a gallery field.
My question is how can I get the count of the sub fields/gallery items.
I need to echo the count within the main loop?
Thanks
Josh
Dbranes answers:
Hi,
If I understand the question right, you could try
$count_document_downloads = count(get_field('document_downloads'));
or
<!-- begin repeater field -->
<?php if ( get_field('document_downloads') ): ?>
<?php $count_document_downloads=0;?>
<?php while( has_sub_field('document_downloads') ): ?>
<?php $count_document_downloads++;?>
<?php the_sub_field('document_name'); ?>
<?php endwhile; ?>
<?php echo "count_document_downloads=". $count_document_downloads;?>
<?php endif; wp_reset_query(); ?>
<!-- end -->
to count.
Hope this helps.
Josh Cranwell comments:
This...
$count_document_downloads = count(get_field('document_downloads'));
...works great, very simple. Please see below.
Remember I am using this in my main loop, not the custom field loops.
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php
$count_document_downloads = count(get_field('document_downloads'));
$count_image_downloads = count(get_field('image_downloads'));
$count_video_downloads = count(get_field('document_downloads'));
?>
<?php if ($count_document_downloads) { ?>
Documents: <span class="count"><?php echo $count_document_downloads; ?></span>
<?php } ?>
<?php if ($count_image_downloads) { ?>
Images: <span class="count"><?php echo $count_image_downloads; ?></span>
<?php } ?>
<?php if ($count_video_downloads) { ?>
Videos: <span class="count"><?php echo $count_video_downloads; ?></span>
<?php } ?>
<?php endwhile; ?>
<?php endif; wp_reset_query(); ?>
I've noticed something weird, if all values are null, it I get a value of 1 returned for everything.
For example, I have 11 images. I get 11 returned for images, then null for documents and videos. Great.
But if I have no images, no docs, no videos = I get a 1 returned for everything.
Any ideas why this is happening?
Thanks
Dbranes comments:
in the last line of
<?php
$count_document_downloads = count(get_field('document_downloads'));
$count_image_downloads = count(get_field('image_downloads'));
$count_video_downloads = count(get_field('document_downloads'));
?>
you got 'document_downloads' instead of 'video_downloads'.
But you could try to replace the above with
<?php
$tmp=get_field('document_downloads');
$count_document_downloads=(is_array($tmp))? count($tmp) : 0;
$tmp=get_field('image_downloads');
$count_image_downloads=(is_array($tmp))? count($tmp) : 0;
$tmp=get_field('video_downloads');
$count_video_downloads=(is_array($tmp))? count($tmp) : 0;
?>
if you use count on a variable that is false, you get 1 (mabye that's the case)
so in the above code we check if the variable is an array, if not we set the count to 0.
Dbranes comments:
ps: mabye you should also check
<pre>
<?php
print_r(get_field('document_downloads'));
print_r(get_field('image_downloads'));
print_r(get_field('document_downloads'));
?>
</pre>
in the loop to see what's inside.
Dbranes comments:
better to use var_dump:
<pre>
<?php
var_dump(get_field('document_downloads'));
var_dump(get_field('image_downloads'));
var_dump(get_field('document_downloads'));
?>
</pre>
Josh Cranwell comments:
Thanks, yeah that worked.
Though I might avoid using after all this as it slows my page down on archive pages.
I will use it on my page/post template as it only have to count the one post.
On archive pages it taks longer as it has to could each post.
Thanks for your help.
Arnav Joy answers:
if you want to count gallery item they try this
<!-- begin gallery field -->
<?php $images = get_field('image_downloads'); if( $images ): ?>
<?php $count=0; foreach( $images as $image ): ?>
<?php echo $image['id']; ?>
<?php $count++; endforeach; ?>
<?php echo 'Gallery Item count='. $count;?>
<?php endif; wp_reset_query(); ?>
<!-- end -->
Josh Cranwell comments:
Hello
Thanks for your suggestion but I ended up using count function above.
Thanks