Skip Navigation

[Résolu] use different Content-template for different Category in Post Type

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I would like to apply a different Content Template to posts based on the terms applied to those posts.

Solution: Use the PHP API wpv_filter_force_template to programmatically change the Content Template.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_template

This support ticket is created Il y a 4 années et 9 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 réponses, has 2 voix.

Last updated by Gregor Il y a 4 années et 8 mois.

Assisted by: Christian Cox.

Auteur
Publications
#1306687

I have setup a post Type "Products" with different Categories (Video, Book, ....) and also different field Groups for al Categories.
How can I have the single product page use a different Content-Template?
When creating a ContentTemplate I can only select the Post Type but not a catergory that should use the Template, which means all Categories show in the same Template.

How can I use different templates for different categories?

Thanks
Gregor

#1307013

I now tried to go a different rout and created different Post Types for the Categories:

Post Type 1: Videos
Post Type 2: Books
Post Type 3: XYZ

This gives me the option to have different Content Templates for each CType -which is what I need- but now I have a different Problem:

I try to set up a search on the home page to filter the results but I can not filter by Post Type as it seams 🙁
On a clients website that I made a few years ago (real estate) I used this code and it worked but I can't get it to work on this site:

[wpv-control url_param="wpv-post-type" type="select" values=",video,buch,zitatkarte" display_values="All Types,Video,Buch,Zitatkarte"]

Changing the select does not filter the search results that are shown.

Another problem with this 2. solution: I have set up different categories for each post type and It would be nict to be able to change the category select after changing the PT but this won't be possible either using the CPT solution I believe.

Which is the best was to proceed?

#1307029

Hi first, I would like to show you how to apply different Content Templates to the same post type. We have a filter called wpv_filter_force_template that will programmatically override the template on-the-fly:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_template
You can click the +More button to see an example of how to show a different template based on some conditional code. You can use wp_get_object_terms to inspect the terms currently associated with the post: https://codex.wordpress.org/Function_Reference/wp_get_object_terms
Let me know if you need more information about this approach, since it might be cleaner than new post types.

Second, filtering by post type isn't officially supported. This shortcode won't work by itself, it requires another PHP filter called wpv_filter_query: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

[wpv-control url_param="wpv-post-type" type="select" values=",video,buch,zitatkarte" display_values="All Types,Video,Buch,Zitatkarte"]

Check the site's custom PHP code, and you should find a corresponding PHP filter using wpv_filter_query. If not, you can use this post as a reference: https://toolset.com/forums/topic/post-type-filter/#post-486216
Note that this approach may not work with AJAX.

#1307175

Hello Christian, thanks for your kind help!!
I found the extra function that I was using in the real estate site and now the search by Type works!

Another question:
in the search results I would like to show/print the PostType Name with every result to make clear it is a book or video.

  • PostType, Titel, .....
  • Is there any function available to get the CPT-Name of the listing?

    Thanks
    Gregor

    #1307267

    There's a shortcode you can use wpv-post-type. The documentation for that and other Views shortcodes is available here: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-type

    If you have other questions about that, feel free to open a new ticket. Thanks!

    #1307741

    Thank you Christian, this was very helpful.
    I managed to have it all working as expected!

    Best Regards
    Gregor

    This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.