Attachments plugin sample code (Jonathan Christoper)
-
Hi,
Just wanted to say thanks to Jonathan Christopher for the attachments plugin. I’m sort of wondering why non-image attachments just don’t show up with their associated icons when inserting [gallery] in a post/page but the Attachments plugin does the trick!Some suggestions:
i) Pull the title and caption from the (and description) entered in the standard WP Media as the default for the attachments array.ii) Also allow re-ordering by putting in an integer field like in the WP standard media gallery functionality. The drag and drop re-ordering is awesome but cumbersome when there are a large number of attachments.
Here is some code I added to my template to display the attachments with the Media title – note that I get the filename but then I decided not to use it. I added this in immediately after the_content(). Thought someone might find this useful.
<?php the_content(); ?> <!-- list attachments in post --> <div id="attachments"> <?php $attachments = attachments_get_attachments(); $total_attachments = count($attachments); ?> <?php if( $total_attachments > 0 ) : ?> <?php for ($i=0; $i < $total_attachments; $i++) : ?> <div class="attachments-div"> <?php $tmp_attachment_id = $attachments[$i]['id']; $tmp_post_id = get_post($tmp_attachment_id); $tmp_title = $tmp_post_id->post_title; $tmp_path_array = explode("/",$attachments[$i]['location']); $tmp_array_length = sizeof($tmp_path_array)-1; $tmp_filename = $tmp_path_array[$tmp_array_length]; $j=$i+1; ?> <a href="<?=$attachments[$i]['location']?>" alt="<?=$tmp_title?>" title="<?=$tmp_title?>" target="_blank"> <?php echo "<p>$j) <b>$tmp_title</b></p>"; ?> <p> <?php echo wp_get_attachment_image($attachments[$i]['id'], $size='thumbnail', $icon = true); ?> </p> </a> </div> <?php endfor ?> <?php endif ?> </div>The associated CSS is:
#attachments { margin: 2em; } .attachments-div { position: relative; float: left; padding: 5px; margin: 5px; border: 1px solid #000000; text-align: center; width: 300px; height: 160px; }
The topic ‘Attachments plugin sample code (Jonathan Christoper)’ is closed to new replies.