Skip Navigation

[Resolved] Dynamically changing the template for a single post page PT.2

This support ticket is created 5 years, 4 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 1 reply, has 2 voices.

Last updated by Minesh 5 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#1139758

I am trying to use the code from original thread - https://toolset.com/forums/topic/dynamically-changing-the-template-for-a-single-post-page/. . I modified it to choose Content template depending on post Category, but can't make it work - while insert in functions.php - I've got blank screen. Can you please advise what's wrong in it:

Thanks in advance, Dmitry


add_filter('get_post_metadata', 'sht_template_filter', 1, 4);

function sht_template_filter($dummy, $post_id, $meta_key, $single, $category) {
    if ( is_singular('post') and $meta_key == '_views_template' and $category == 'news-t2' ) {                    //Check the slug for Новости-t2 , then we are viewing a single post ( NEWS ) type
        if ( is_recognized() ) 
		{ return 1367; }                                                                                            
    } 

	else { return null; } // In other case, we return null, which means the content won't be modified
}

#1140204

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - Toolset offers the filter "wpv_filter_force_template" that you can use to dynamically assign the content template you want to assign:

For example:

//Show a specific Content Template applied to a given post for not logged in visitors and in every place where this post appears:
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 == 345 ) { // if the user is not logged in and is trying to view the post with ID 345
        $template_selected = 123; // assign a fixed Content Template with ID 123
    }
    return $template_selected;
}

More info:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_template

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