How to implement "product gallery" feature of woocommerce into a custom post type

I'm wondering if there is a way to borrow the "product gallery" feature of woocommerce and add it to a custom post type in wordpress.

enter image description here

I know i can go through the woocommerce source codes and search and find its nuts and bolts and pieces in the woocommerce plugin folder, put them together and customize them for a specific custom post type. I know these are some of the corresponding files in the woocommerce plugin folder:

  • For its javascript:
    • meta-boxes-product.js
    • single-product.js
  • For rendering its html template:
    • wc-template-functions.php > There is wc_get_gallery_image_html function
    • class-wc-admin-meta-boxes.php
      • add_meta_box( 'woocommerce-product-images', __( 'Product gallery', 'woocommerce' ), 'WC_Meta_Box_Product_Images::output', 'product', 'side', 'low' );
    • class-wc-meta-box-product-images.php
    • etc.

BUT this is prone to errors and bugs. I'm wondering if there is a better, easier, more elegant, less buggy way to implement this feature to a custom post type. Maybe there is a hook and/or a class somewhere that you guys know about and i'm missing here.

Just to clarify my question, i actually have a custom post type it's called "projects". I need a gallery for this custom post type which accepts new images for my custom post type, so i thought i could use the "product gallery" feature from woocommerce instead of writing a gallery from scratch for my custom post type.

Thank you.


Not tested but if you could get the
inside your CPT file try this code:

$product_id = '14';
$product = new WC_product($product_id);
$attachment_ids = $product->get_gallery_image_ids();

foreach( $attachment_ids as $attachment_id ) 
      // Display the image URL
      echo $Original_image_url = wp_get_attachment_url( $attachment_id );

      // Display Image instead of URL
      echo wp_get_attachment_image($attachment_id, 'full');


This should work!

Similar questions

Option to feature custom post type on home page
I am wondering what's the best way to give a user the option to feature a custom post type on the home page. I have a Case Study custom post type and some of them need to be feautured on the home page. Ideally when creating the case study there would be like a checkbox labeled "Feature on home page". Once that is selected the home page will show th...
Feature image Gallery made from Custom Post type + Categories
Hey guys im trying to find a way to create a little gallery (Will post a image of what I need) Using Custom post type's Featured Image's where I can swap the image's based on category I have 0 idea how to go about this and really need some advice. I can probably create the layout my self, Im just trying to keep this as simple as possible for the ne...
display multiple feature images through custom post type with shortcode
I have a shortcode/multiple feature image problem I'm hoping for a bit of help on. My custom post type called rubow_medarbejder is displayed on a page with a shortcode. This works fine. I need to add a second feature image. I've used the Multiple Featured Images plugin to do this. Problem is I can't get the image to display through my shortcode. kd...
How to implement AJAX post navigation into WordPress?
I am developing a wordpress page template exactly like this. I have completed the design and have loaded the template once user goes to this page which using my template. I want to all posts in more article list open in this template without refreshing the page. but when i click on any post in the list it opens in a new page which is off course dif...
How to implement jquery-ui autocomplete into custom fields?
I currently have a custom fields for enter State name. I want to use the autocomplete from jquery-ui in this fields. However, I failed. This is the first time I using jquery-ui, and I am not so familiar with php and jquery. I hope that you guys can give me some advises. This was the code that I use: Above is the example code that I use to call the ...
Best practice way to implement custom sections into a WordPress theme
I'm having trouble figuring out the best way to implement custom sections in a WordPress theme. For example I'm making a theme that will have four featured sections on the homepage. These sections will be in there own area, separate from the page content. I know this could be done by adding four widget positions, or one widget position and making t...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.