Skip Navigation

[Resolved] Using different Content Templates for the same CPT

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to apply different Content Templates to the same Custom Post Type depending on the current User's custom field value, or User role, or some other criteria.

Solution: We offer a filter that will allow you to programmatically override the default Content Template - wpv_filter_force_template:

add_filter( 'wpv_filter_force_template', 'dynamic_author_template_for_post_12345', 99, 3 );
function dynamic_author_template_for_posts( $template_selected, $id, $kind ) {
    if ( $id == 12345 ) { // if the user is trying to view the post with ID 12345
      $template_selected = get_the_author_meta('wpcf-template-field-slug');
    }
    return $template_selected;
}

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

This support ticket is created 7 years, 2 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
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 3 replies, has 3 voices.

Last updated by John Johansen 7 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#570563

Tell us what you are trying to do?

I am trying to display the same CPT posts using several different layouts depending on whether the Author of the post belongs to a specific "group" of user. A "group" here could be defined in an number of ways: custom fields, membership, roles, etc.

Each group would have a different layout assigned to them. Ideally, I would like to be able to define things such as if an author belongs to one of those groups, then the layout assigned to their post follows a different content template.

Is something like this possible? If not, how could I try to get as close to this functionality as possible?

Many thanks!

Best,
- Julien

#570774

There's not a way to programmatically choose between Layouts, but we offer a filter that will allow you to programmatically apply different Content Templates to a post - wpv_filter_force_template. You can find more information about it here:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_template

In PHP, you could find information about the post author, like the author's role, or a custom field on the author's profile, and use that in your conditional to determine which Content Template should be applied to a post.

Example:

add_filter( 'wpv_filter_force_template', 'dynamic_author_template_for_post_12345', 99, 3 );
function dynamic_author_template_for_posts( $template_selected, $id, $kind ) {
    if ( $id == 12345 ) { // if the user is trying to view the post with ID 12345
      $template_selected = get_the_author_meta('wpcf-template-field-slug');
    }
    return $template_selected;
}
#571248

Many thanks Christian, this answers perfectly my question and the code sample is very helpful in understanding how to take it on from here 🙂

Many thanks!

Best,
- Julien

#606563

"There's not a way to programmatically choose between Layouts, but... "

I would like clarification of this particular assertion, mainly because I have recently implemented the 'get_layout_id_for_render' as a filter in order to define which layout to use based on certain conditions (in my case, user assignments using the Groups plugin). So far, it has worked well for each of several custom post types and several user types to display the appropriate Layout (in order to control which content is displayed as well as any optional functional elements). In short, I let WordPress assign the initial template (and Layout id) based on the custom post type, and then I override the Layout id assignment based on various conditions.

That said, when would it be more appropriate to force the use of a particular template rather than overriding the Layout?
Perhaps if the Layout plugin were not being used?