Trouble getting attachments to show in custom post types
-
Hello,
I’m trying to get attachments to show in a custom post type. I’ve got the attachments metabox showing for this post type in the WP backend, with the only modification to the sample code from your usage notes being the addition of my post type to the post type array, i.e. ‘post_type’ => array( ‘post’, ‘page’, ‘comm_event’).
function my_attachments( $attachments ) { $args = array( 'label' => 'My Attachments', 'post_type' => array( 'post', 'page', 'comm_event' ), 'filetype' => null, // no filetype limit 'note' => 'Attach files here!', 'button_text' => __( 'Attach Files', 'attachments' ), 'modal_text' => __( 'Attach', 'attachments' ), 'fields' => array( array( 'name' => 'title', // unique field name 'type' => 'text', // registered field type (field available in 3.0: text) 'label' => __( 'Title', 'attachments' ), // label to display ), array( 'name' => 'caption', // unique field name 'type' => 'text', // registered field type (field available in 3.0: text) 'label' => __( 'Caption', 'attachments' ), // label to display ), array( 'name' => 'copyright', // unique field name 'type' => 'text', // registered field type (field available in 3.0: text) 'label' => __( 'Copyright', 'attachments' ), // label to display ), ), ); $attachments->register( 'my_attachments', $args ); // unique instance name } add_action( 'attachments_register', 'my_attachments' );
I’m having difficulty getting the attachments to show in pages I create though. Your sample code for showing attachments( pasted at the bottom of this for clarity) is not showing anything for this custom post type. I’ve even tried pasting it into WP’s standard post’s single.php (where it works) and using the exact same code (i.e. the whole single.php file) for the custom post type’s template and it still won’t show.
I’m assuming there’s something obvious I’ve failed to do. Could you shed any light on it?
Thanks,
Jonathan
<?php $attachments = new Attachments( 'attachments' ); /* pass the instance name */ ?> <?php if( $attachments->exist() ) : ?> <h3>Attachments</h3> <ul> <?php while( $attachment = $attachments->get() ) : ?> <li> <pre><?php print_r( $attachment ); ?></pre> </li> <?php endwhile; ?> </ul> <?php endif; ?>
- The topic ‘Trouble getting attachments to show in custom post types’ is closed to new replies.