REST API 不呈现短代码,因此您必须强制它。创建一个文件 wp-content/mu-plugins/render-xyz-shortcodes.php。您可能必须为其创建 mu-plugin,因为默认情况下它不存在。
<?php
/**
* Render the shortcode in wp-json API
*/
add_action( 'rest_api_init', function () {
register_rest_field(
'post',
'content',
array(
'get_callback' => 'render_xyz_do_shortcode',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
'post',
'excerpt',
array(
'get_callback' => 'render_xyz_do_shortcode',
'update_callback' => null,
'schema' => null,
)
);
});
function render_xyz_do_shortcode( $object, $field_name, $request ) {
global $post;
$post = get_post($object['id']);
$output = array();
//Apply the_content's filter, one of them interpret shortcodes
switch( $field_name ) {
case 'content':
$output['rendered'] = apply_filters( 'the_content', $post->post_content );
break;
case 'excerpt':
$output['rendered'] = apply_filters( 'the_excerpt', $post->post_excerpt );
break;
}
$output['protected'] = false;
return $output;
}