WordPress Attachments

Attachments is a WordPress plugin that allows you to append any number of items from your Media Library to any Page or Post. It is the official replacement and continuation of Post Gallery, which is no longer supported.

Installation

  1. Download the file and unzip it. Upload the ‘attachments’ folder and its subfolders to your ~/wp-content/plugins/ directory.
  2. Activate the plugin through the ‘Plugins’ admin menu in WordPress

Usage

This plugin does not automatically integrate with your theme. You will need to edit the files in which you’d like to reference Attachments.

To pull all Attachments for any Page or Post, you can fire attachments_get_attachments(). The function has one optional parameter, which can be a particular Post ID (if used outside The Loop). If the plugin is fired inside The Loop, it will pull all Attachments for the current post. attachments_get_attachments() returns an array consisting of all available Attachments.

<?php $attachments = attachments_get_attachments(); ?>

You will be provided an associative array with which to work in your template. Here’s a full example:

<div id="attachments">
  <?php 
    $attachments = attachments_get_attachments();
    $total_attachments = count($attachments);
  ?>
  <?php if( $total_attachments > 0 ) : ?>
    <ol>
      <?php for ($i=0; $i < $total_attachments; $i++) : ?>
        <li>
          <h3><?=$attachments[$i]['title']?></h3>
          <p><?=$attachments[$i]['caption']?></p>
          <p><small>Attachment ID: <?=$attachments[$i]['id']?></small></p>
          <p><code><?=$attachments[$i]['location']?></code></p>
          <p><?=$attachments[$i]['mime']?></p>
        </li>
      <?php endfor ?>
    </ol>
  <?php endif ?>
</div>

Screencast