0

I'm not new to WordPress but I have a question for the gutenberg api.

I want to get a single post by id with the rendered post template for this ID. But I don't know how to archieve this problem because the codex has not that information for me.

So when I request the https://example.com/wp-json/wp/v2/posts/1 I got a not the default post template block rendered content html but the post_content rendered content. So my question is how can I access the detail element (single view) from a custom post type rendered in HTML. So we in classic themes i can easily get this rendered content with get_template_part and the ID of the post.

So how can I access the rendered html content for a single view from a post.

1 Answer 1

0

To achieve the desired results you need to create a custom REST API endpoint that returns the rendered post.

In order to create custom REST API endpoint please add the given code to your theme's functions.php file or in a custom plugin.


    function get_rendered_post_template_data($data) {
        $post_id = $data['id'];
        $post    = get_post($post_id);
    
        if ( !$post ) {
            return new WP_Error( 'no_post', 'Post not found', array('status' => 404) );
        }
    
        setup_postdata($post);
    
        ob_start();
        // Here you can adjust the template part if you have specific ones for different post types
        get_template_part('single', $post->post_type);
        $rendered_html = ob_get_clean();
    
        wp_reset_postdata();
    
        return array(
            'id'      => $post_id,
            'title'   => get_the_title($post),
            'content' => $rendered_html,
        );
    }
    
    function register_custom_routes() {
        register_rest_route( 'custom/v1', '/rendered-post-data/(?P<id>\d+)', array(
            'methods'  => 'GET',
            'callback' => 'get_rendered_post_template_data',
        ));
    }
    
    add_action( 'rest_api_init', 'register_custom_routes' );

We can access the rendered post by making a GET request to the custom endpoint we have created.

For example, if your site is https://domain-name then it should be accessed by using https://domain-name/wp-json/custom/v1/rendered-post-data/1

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but this is only working for classic themes not for block themes. The get_template_part is not working with block themes. This was already tried by me.
Could you please try to check with the given code. $rendered_html = apply_filters('the_content', $post->post_content); instead of get_template_part
If you want full link you can get that with the help of wp_remote_get. $web_link = get_permalink( $post_id ); $web_res = wp_remote_get( $web_link ) if ( ! is_wp_error( $web_res ) ) { $item['body'] = wp_remote_retrieve_body( $web_res ); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.