Creating Photo Gallery System with Custom Post Type

I’m creating a movie blog using WordPress.

I have Movie and Trailer custom post types and I want to add a Photo feature for this and I want to add a relationship between this photos and Movie CPT.

What the best way to handle this? Is custom field the answer? And manually add the movie post id on the custom field?

1 Answer
1

Yes, Custom Fields is a good answer for that.

To make things easier there’s a couple of plugins that can automate the process and create a nice interface for that.

One is Advanced Custom Fields, but its Repeatable Field is a premium add-on, so I’ll show how to do it with another one.

Custom Content Type Manager

This plugin also manages Custom Post Types, but, if you already created yours, they’ll appear as “externally created”.

Anyway, it also handles Custom Fields and you can assign them to any CPTs.

Bellow, a snapshot of a Gallery CPT that has only the Title enabled, plus one repeatable field for Images and another field for RelationShip (this can be any post type: post, page, cpt, attachment).

enter image description here

You’ll have to read the plugin documentation on how to implement those CF’s with your Theme code.

Sample code on how to retrieve the repeatable image field named gallery_img:to_array:

$the_imgs = get_custom_field('gallery_img:to_array');
foreach( $the_imgs as $img )
{
    list( $url, $width, $height ) = wp_get_attachment_image_src( $img, 'thumbnail' );
    echo '<img src="' . $url . '" width="' . $width . '" height="' . $height . '"><br />'; 
}

Leave a Comment