logo

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

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

attachment image View Attachment

Rich Staats | 08/11/11 at 11:07am | Edit


(1) Possible Answers Submitted...

  • avatar
    Last edited:
    08/11/11
    11:11am
    Utkarsh Kukreti says:

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

    • 08/11/11 11:16am

      Rich Staats says:

      It's using the WPAlchemy Class 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

    • 08/11/11 11:17am

      Rich Staats says:


      <?php

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

      $count = count($attachments);

      ?>


      was part of my attempt to find a solution myself.

    • 08/11/11 11:18am

      Utkarsh Kukreti says:

      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>

    • 08/11/11 11:22am

      Rich Staats says:

      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

    • 08/11/11 11:35am

      Utkarsh Kukreti says:

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

    • 08/11/11 11:38am

      Utkarsh Kukreti says:

      Try this instead for the attachments = line.


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


    • 08/11/11 12:33pm

      Rich Staats says:

      ypu that worked. thanks for the quick response.

    • 08/11/11 12:35pm

      Rich Staats says:

      how do i award you the money?

    • 08/11/11 12:37pm

      Utkarsh Kukreti says:

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

This question has expired.



Rich Staats voted on this question.



Current status of this question: Completed