Skip Navigation

[Resolved] Content Template-ID / Usage of Content Templates

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.

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
- 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 Waqar 5 years, 4 months ago.

Assisted by: Waqar.

Author
Posts
#1293667
toolset_content_templates.jpg

Hi there,

I´d like to render templates of posts (custom post-type "siegel").
The following function works for one specific post and template. But how do I get/read the $content_template_id dynamicly.

- - - - - - - - - - - - -

add_action( 'rest_api_init', 'create_api_posts_meta_field' );
function create_api_posts_meta_field() {

// register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
register_rest_field( 'siegel', 'post-meta-fields', array(
'get_callback' => 'get_post_meta_for_api',
'schema' => null,
)
);
}

function get_post_meta_for_api( $object ) {
//get the id of the post object array
$post_id = $object['id'];

//return the post meta
return get_post_meta( $post_id );

//Renders the post $mypost using the Content Template with ID
return render_view_template( $post_id, $mypost );
}

// register a new field to hold the rendered template
register_rest_field( 'siegel', 'post-rendered-template', array(
'get_callback' => 'get_post_rendered_template_for_api',
'schema' => array(
'description' => __( 'Rendered Siegel Content Template' ),
'type' => 'string'
),
)
);

// render the content template for this siegel post using render_view_template
// then add it to the data model
function get_post_rendered_template_for_api( $object ) {
global $current_user;
$content_template_id = 957;
$the_post = get_post($object['id']);
$output_siegel_s = render_view_template( $content_template_id, $the_post, $current_user );
return $output_siegel_s;
}

- - - - - - - - - - - - -

EXPLANATION

Example that works:

Post-Type: siegel
Post-ID: 958 (Siegel UHC Medien S)
Content Template-ID: 957 (Template for Siegel S)
Output-Example: hidden link
JSON-Editor: hidden link

Example that does not work:

Post-Type: siegel
Post-ID: 959 (Siegel UHC Medien XS)
Content Template-ID: should be => 956 (Template for Siegel XS)

First, a variable must be inserted for the Content Template-ID (How do you read this out?)

On the other hand, it seems to me that only one content template can be assigned to the CPT siegel as usage (screenshot).
Right? How to change that?

Siegel XS => Content Template XS
Siegel S => Content Template S
Siegel M => Content Template M

Thanks for your support.
Regards,
Uwe

#1293911

Hi Uwe,

Thank you for contacting us and I'd be happy to assist.

There are a number of ways, different content templates can be attached manually to different content items, as explained in this guide:
https://toolset.com/documentation/user-guides/setting-view-templates-for-single-pages/

If you're interested in programmatically overriding content templates for selective posts, you can use the filter "wpv_filter_force_template":
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_template

Example from the documentation:


//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 that contains a static text
    }
    return $template_selected;
}

As needed, you can change the conditional tags, to target specific pages/posts.
( ref: https://codex.wordpress.org/Conditional_Tags )

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1296807

Please do not close the thread yet. I will come to test it next week.
Thanks

#1297265

Hi Uwe,

Sure, the ticket will be open for the next couple of weeks and feel free to share your results.

For a different question/concern, you're welcome to start a new ticket.

regards,
Waqar