我们可以实现自己的端点:
https://example.tld/wpse/v1/frontpage
下面是一个简单的演示(PHP 5.4+):
<?php
/**
* Plugin Name: WPSE - Static Frontpage Rest Endpoint
*/
namespace WPSE\\RestAPI\\Frontpage;
\\add_action( \'rest_api_init\', function()
{
\\register_rest_route( \'wpse/v1\', \'/frontpage/\',
[
\'methods\' => \'GET\',
\'callback\' => __NAMESPACE__.\'\\rest_results\'
]
);
} );
function rest_results( $request )
{
// Get the ID of the static frontpage. If not set it\'s 0
$pid = (int) \\get_option( \'page_on_front\' );
// Get the corresponding post object (let\'s show our intention explicitly)
$post = ( $pid > 0 ) ? \\get_post( $pid ) : null;
// No static frontpage is set
if( ! is_a( $post, \'\\WP_Post\' ) )
return new \\WP_Error( \'wpse-error\',
\\esc_html__( \'No Static Frontpage\', \'wpse\' ), [ \'status\' => 404 ] );
// Response setup
$data = [
\'ID\' => $post->ID,
\'content\' => [ \'raw\' => $post->post_content ]
];
return new \\WP_REST_Response( $data, 200 );
}
成功的响应如下:
{"ID":123,"content":{"raw":"Some content"}}
错误响应如下所示:
{"code":"wpse-error","message":"No Static Frontpage","data":{"status":404}}
然后,我们可以通过应用
the_content
滤器