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