• Resolved shecodes

    (@shecodes)


    I’ve created two custom post types ‘galleries1’ and ‘galleries2’.

    This is because the website operates two different services that are styled differently. And I want to be able to use the built-in gallery function in WordPress but style the gallery differently depending on whether it’s in section ‘1’ of the website or section ‘2’.

    So far I have been able to create templates as follows:

    2 styles of index.php
    archive-galleries1.php and archive-galleries2.php
    – showing all the available galleries for that section.

    2 styles of single.php
    single-galleries1.php and single-galleries2.php
    – showing all the thumbnails within a particular gallery

    I’ve now hit a wall where clicking on an individual thumb to its attachment page leads to the image.php file. If I remove this it leads to the content-single.php file. What template should be used to enable me to create two styles of template for the single image attachment page? I’m not sure if this is possible as I’ve noticed I am linking to an attachment from the media library so does this mean that I can’t relate back to the fact that it was attached to a specific custom post type ‘galleries1’ or ‘galleries2’ ?

    I’m not sure if I’ve explained myself well, but would appreciate any help or guidance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    What you could do is create a custom image.php template that determines the post type of the parent post and styles the output accordingly. Depending on how you manage attachments, there may be a condition where the image’s parent is not the post that the user was viewing. If that’s possible, you should determine the parent by checking the referring page URL instead of the listed parent in the DB.

    If you prefer, you could create separate custom image templates and manage which is loaded with the ‘image_template’ filter. In this case your callback doesn’t have the current post data to work from, but it can be had from the global $wp_query object, which can easily be retrieved with get_queried_object().

    Thread Starter shecodes

    (@shecodes)

    Thank you so much for pointing me in the right direction, you are genius.

    By using the wp_prepare_attachment_for_js I was able to locate ‘uploadedTo’ which gave me the ID of the parent gallery for the image ($gallery). I could then check the custom post type by using get_post_type on the parent gallery ID ($gallery). The value of this ($gallery_type) is either ‘galleries1’ or ‘galleries2’.

    $meta_capture = wp_prepare_attachment_for_js( get_post_thumbnail_id( get_the_ID() ));
                                                            $gallery = $meta_capture['uploadedTo'];
                                                            $gallery_type = get_post_type( $gallery );

    So I can now load two different content templates depending on the value of $gallery_type. Perfect!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Single image attachment template for custom post type gallery’ is closed to new replies.