I was told, one can use the field "_views_template" to store the TOOLSET-Template for a post.
But
[cred_field field="_views_template" output="bootstrap"]
or anything similar just gives me an error.
How would that work?
Would it be able to let users choose between some templates by radio or so?
Hi,
Thank you for contacting us and I'd be happy to assist.
To offer a content template selection field in a post form, you'll need to use a generic field, for example:
( ref: https://toolset.com/documentation/programmer-reference/forms/cred-shortcodes/#cred_generic_field )
[cred_generic_field type='radio' field='select-template']
{
"required":1,
"default":[],
"options":[{"value":"123","label":"Template 1"},{"value":"456","label":"Template 2"}]
}
[/cred_generic_field]
Note: In the generic field's options attribute, replace the values and labels, to match your content template IDs and titles.
Next, you'll need a custom function attached to the "cred_save_data" that gets the selected template's ID from the generic field and sets it as the "_views_template" custom field value:
( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data )
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==12345)
{
if (isset($_POST['select-template']))
{
update_post_meta( $post_id, '_views_template', $_POST['select-template'] );
}
}
}
Note: You'll replace '12345' with your actual form's ID.
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Ok, this seiems to work. Thanks 🙂
And how set that radio buttons to the CURRENT page template when loading a CRED form to EDIT a post?
Thank you! 🙂
Thanks for the update and glad that it worked.
Assuming that you're using the edit form on the same page/post that is being edited, you'll need a custom shortcode that returns the current post/page's assigned content template ID:
( ref: https://toolset.com/documentation/programmer-reference/views-api/#has_wpv_content_template )
add_shortcode('get_current_CT_ID', 'get_current_CT_ID_func');
function get_current_CT_ID_func() {
global $post;
$has_ct_assigned = has_wpv_content_template( $post->ID );
if ( $has_ct_assigned > 0 ) {
return $has_ct_assigned;
}
}
After that, you can use that shortcode in the default attribute of the generic field:
[cred_generic_field type='radio' field='select-template']
{
"required":1,
"default":"[get_current_CT_ID]",
"options":[{"value":"123","label":"Template 1"},{"value":"456","label":"Template 2"}]
}
[/cred_generic_field]
As a result, when the edit form will load, the currently assigned content template will be automatically selected in the generic field.
My issue is resolved now. Thank you!
GREAT SUPPORT, THANK YOU, WAQAR!
This should be native part of Toolset, it´s so usefull!