Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm doing research on custom post types for a Wordpress site I'm going to be putting together for a client of mine. The basics of the Custom Post Type seem pretty straight forward. What I'm trying to determine is the best way that I could have an image gallery as part of my custom post type.

For example, a custom post type would be a car. In addition to the details of the car (make, model, year, mileage, etc.) there will be 1-n images associated with it that should be part of the post (modified inside the post, removed along with the post, etc.) Ideally I'd want thumbnail images that open up to a lightbox of some sort.

Hopefully this is something that can be accomplished without totally hacking up Wordpress.

share|improve this question
    
WordPress has a 'gallery' built in that should work for a CPT (I checked, it does.) and there are piles of gallery plugins in the WordPress repo. Have you looked into any of them? How far along have you gotten? What have you tried? –  s_ha_dum Nov 6 '12 at 15:21
    
I am still in the research stage. I'll hopefully be working on setting up a test this weekend with a CPT and a gallery. –  Don Nov 8 '12 at 3:28

1 Answer 1

up vote 1 down vote accepted

Custom Post Types can have image attachments like other post types. A simple way to display them is via the gallery shortcode. There are several plugins out there to enhance the gallery output, or you can also display attachments in a single post yourself via the API, for example with get_posts.

The only requirement you won't get out of the box is deletion of attachments when the parent post is deleted, as images can be used in other posts so that could potentially break things. If that's not a concern, you can delete attachments on post delete with a bit of code hooked to deletion:

function delete_post_children($post_id) {
    global $wpdb;

    $ids = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_parent = $post_id AND post_type = 'attachment'");

    foreach ( $ids as $id )
        wp_delete_attachment($id);
}
add_action('delete_post', 'delete_post_children');

taken from http://core.trac.wordpress.org/ticket/12108

share|improve this answer
    
Thanks, I'll give this a shot! –  Don Nov 8 '12 at 3:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.