Skip Navigation

[Resolved] View to display different CPTs using different Content Templates for each CPT

This support ticket is created 5 years, 9 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Ben 5 years, 9 months ago.

Assisted by: Waqar.

Author
Posts
#1201293

Ben

I have thought about this issue and tried to visualise it the best I can for myself but I can't figure it out. Apologies if the answer is very simple, I just can't get there myself!

I am aware that you can set a View to query multiple different CPTs and then design the output within the Loop Editor. I wish to be able to do this but to use a different Content Template for each returned result, dependant on the CPT. Is this possible and how would I achieve this?

For example; for every result that is CPT of "Author" I would like to use an "Author" Content Template designed just for this CPT and for every result that is CPT of "Book" then I would like to use a "Book" Content Template.

I hope this makes sense. Let me know if there is anything else you need.

#1201294

Ben

I was looking for some sort of conditional to test the CPT type but I couldn't find anything like this.

#1201549

Hi Ben,

Thanks for asking! I'd be happy to help.

To show different content templates for the different CPTs returned through a single view, you can use "wpv-post-type" type shortcode, which returns the current post's post type:
https://toolset.com/documentation/user-guides/views-shortcodes/#vf-153408

This shortcode can be nested inside a conditional shortcode, to show different content templates, accordingly:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

For example, in your view's main content template you can add:


[wpv-conditional if="( '[wpv-post-type]' eq 'author' )"]
[wpv-post-body view_template="ct-for-author"]
[/wpv-conditional]

[wpv-conditional if="( '[wpv-post-type]' eq 'book' )"]
[wpv-post-body view_template="ct-for-book"]
[/wpv-conditional]

Feel free to replace "author" and "book" with your actual CPT slugs and "ct-for-author" and "ct-for-book", with actual content template slugs.

I hope this helps.

regards,
Waqar

#1202549

Ben

Thank you for your reply Waqar.

This is perfect and exactly what I wanted to do!