WordPress.org

Ready to get started?Download WordPress

Forums

Attachments
[resolved] Trouble getting attachments to show in custom post types (3 posts)

  1. Jonathan Sutcliffe
    Member
    Posted 1 year ago #

    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; ?>

    http://wordpress.org/extend/plugins/attachments/

  2. Jonathan Sutcliffe
    Member
    Posted 1 year ago #

    Arg, I've discovered what the silly thing I was doing wrong was. I'd failed to notice that the attachments that were working in single.php were using the default instance, called "attachments", and the comm_event template wanted to use "my_attachments," so what I needed to do was:

    $attachments = new Attachments( 'my_attachments' );

  3. Jonathan Christopher
    Member
    Plugin Author

    Posted 1 year ago #

    Glad you got it sorted out, nice! Thank you for following up too!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.