Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

echo the number of images attached to a page in the media gallery WordPress

  • SOLVED

Attached you'll see an image that shows a custom media metabox to upload images. It uses the WP media manager and is part of WPAlchemy's metabox class (farinspace.com). You'll notice the text "There are XX images in your gallery." I'd love to replace the XX with the either, 0 if there are no images, and the number of images attached if there are any. I'd most likely wrap these images in span tags with an appropriate class so that I can style them, but that's about it.

Any help is appreciated.

Rich

Answers (1)

2011-08-11

Utkarsh Kukreti answers:

What code have you used to add the content in the screenshot?


Rich Staats comments:

It's using the [[LINK href="http://www.farinspace.com/wpalchemy-metabox/"]]WPAlchemy Class[[/LINK]] from farinspace.com, but here is the code to display the metabox:

<?php global $custom_media_access; ?>
<div class="my_meta_control metabox">
<?php
$attachments = (array('post_type' => 'attachment' ));
$count = count($attachments);
?>

<?php $custom_media_access->setGroupName('nn')->setInsertButtonLabel('Insert')->setTab('type'); ?>
<div class="media_mb">
<p><?php echo $custom_media_access->getButton(array('label' => 'Add or Edit Images')); ?> Your Gallery has XX images in the Slide Show</p>
</div>

</div>


Let me know if you need something more.

Rich


Rich Staats comments:


<?php

$attachments = (array('post_type' => 'attachment' ));

$count = count($attachments);

?>


was part of my attempt to find a solution myself.


Utkarsh Kukreti comments:

Try this:

<?php global $custom_media_access, $post; ?>
<div class="my_meta_control metabox">
<?php
$attachments = get_posts(array('post_type' => 'attachment', 'post_parent' => $post->ID ));
$count = count($attachments);
?>

<?php $custom_media_access->setGroupName('nn')->setInsertButtonLabel('Insert')->setTab('type'); ?>
<div class="media_mb">
<p><?php echo $custom_media_access->getButton(array('label' => 'Add or Edit Images')); ?> Your Gallery has <?php echo $count ?> images in the Slide Show</p>
</div>

</div>


Rich Staats comments:

for some reason, and this where i was having the problem, is that it echos "1" even though currently there are 3 images currently attached to that page


Utkarsh Kukreti comments:

Could I get wp-admin access to the site you tried this on, just to confirm?


Utkarsh Kukreti comments:

Try this instead for the attachments = line.


$attachments = get_posts(array('post_type' => 'attachment', 'post_parent' => $post->ID, 'post_status' => 'inherit' ));


Rich Staats comments:

ypu that worked. thanks for the quick response.


Rich Staats comments:

how do i award you the money?


Utkarsh Kukreti comments:

http://codewi.se/2011/04/22/voting-assign-prize-money/