Skip Navigation

[Resolved] Set template per post for a custom post type

This support ticket is created 3 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 7 replies, has 2 voices.

Last updated by Minesh 3 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#1941863

Hi guys!
I have a question.
I created a custom post type called 'products'. Every product has its own features and design and therefore needs his own template.
Is it possible to set select a template for every post (like we can in pages).
Thanks!
Marc

#1942207

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Yes, its possible.

There are two ways, either you can use the [wpv-conditional] or use the hook but hook is more efficient.

What you have to do it, you will require to create different content templates per your post and then you can switch the content template based on your post ID using the view's filter hook: wpv_filter_force_template
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_template

For exmaple:

add_filter( 'wpv_filter_force_template', 'prefix_fixed_content_for_visitors', 99, 3 );
  
function prefix_fixed_content_for_visitors( $template_selected, $id, $kind ) {
    if ( !is_user_logged_in() && $id == 999 ) { 
        $template_selected = 1; 
    } else  if ( !is_user_logged_in() && $id == 888 ) { 
        $template_selected = 2; 
    }  if ( !is_user_logged_in() && $id == 777 ) { 
        $template_selected = 3; 
    }
    return $template_selected;
}

Where:
- $id is the post ID and you have to assign the original content template ID instead of 1,2 and 3.

I hope this is clear 🙂

#1948757

Hi Minesh, thanks a lot!
Another important part (that i forgot to mention) is that the field sets are different.
So i would love to have the option to select a template and than see the related fields (just like in pages).
Is that possible?

#1948831

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

So i would love to have the option to select a template and than see the related fields (just like in pages).
Is that possible?
==>
I'm not sure what exact requirement you have.

Do you mean that you want to allow users to select content template from backend while the edit their post/page? If you can share more details on your requirement with example, I'll be able guide you in the right direction.

#1948969

Hi Minesh,
Sorry for explaining myself not clear. The idea is simple.

When you create a new page. You pick a template and after saving the related fields are shown for that particular template.
That same pattern (pick a template + save the post + see the correct field set) i want to implement for a custom post type. I don't have the 'template' metabox (that is available in pages) in a custom post type, so is there a way to achieve this.

Thanks!
Marc

#1949033

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

That template box is from WordPress not the Toolset content template.

I'm not sure why you want that as you can use the hook to assign the correct content template created using Toolset views/blocks. As shows with the example:
- https://toolset.com/forums/topic/set-template-per-post-for-a-custom-post-type/#post-1942207

If post ID is 999 assign it content template 1 and so on.

Having said that, lets say you have 100 posts then you will have 100 different content template. That is too bulky.

#1952105

Hi Minesh, thanks and I still think i haven't explained myself well enough.

Imagine a product detail page that looks completely different for every product and contains completely different types of information. In order to manage those, we need completely different fields for every product. I'm looking for a way to set a different template for every product and see a field set that is specific for that product as well.
Is that possible?

Thanks!

ps. we won't have 100's of posts, there will only be max 15 products.

#1953569

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

So the solution I shared is the best match for you.
=> https://toolset.com/forums/topic/set-template-per-post-for-a-custom-post-type/#post-1942207

So, go to Toolset => Content templates
- Add new content template for product X (dont assign this content template to any post type) and add your require fields, example field 1 and 2 and 3
- Add new content template for product Y(dont assign this content template to any post type) and add your require fields, example field 4 and 5 and 6
- Add new content template for product Z(dont assign this content template to any post type) and add your require fields, example field 7,8,9

Now, add the code I shared in my previous reply:

add_filter( 'wpv_filter_force_template', 'prefix_fixed_content_for_visitors', 99, 3 );
   
function prefix_fixed_content_for_visitors( $template_selected, $id, $kind ) {
    if ( !is_user_logged_in() && $id == 999 ) { 
        $template_selected = 1; 
    } else  if ( !is_user_logged_in() && $id == 888 ) { 
        $template_selected = 2; 
    }  if ( !is_user_logged_in() && $id == 777 ) { 
        $template_selected = 3; 
    }
    return $template_selected;
}

- Target the $id which is your post ID and based of your post ID assign the different content template IDs.

I hope this is clear now.