Wordpress 为什么Divi短代码不';在wp-json中不呈现?
我正试图使用Wordpress 为什么Divi短代码不';在wp-json中不呈现?,wordpress,wordpress-rest-api,Wordpress,Wordpress Rest Api,我正试图使用内容过滤器来处理Divi短代码,但短代码仍然出现。具体来说,如有必要,请选择“等”部分、“等”列、“等”文本 add_action( 'rest_api_init', function () { register_rest_field( 'post', 'content', array( 'get_callback' => 'ap3_divi_do_shortcodes',
内容
过滤器来处理Divi短代码,但短代码仍然出现。具体来说,如有必要,请选择“等”部分、“等”列、“等”文本
add_action( 'rest_api_init', function () {
register_rest_field(
'post',
'content',
array(
'get_callback' => 'ap3_divi_do_shortcodes',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
'post',
'excerpt',
array(
'get_callback' => 'ap3_divi_do_shortcodes',
'update_callback' => null,
'schema' => null,
)
);
});
function ap3_divi_do_shortcodes( $object, $field_name, $request ) {
global $post;
$post = get_post($object['id']);
// Set is_singular to true to avoid "read more issue"
// Issue come from is_singular () in divi-builder.php line 73
global $wp_query;
$wp_query->is_singular = true;
$output = array(
'protected' => false
);
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;
}
return $output;
}
我自己想出来的。我需要调用
et\u builder\u add\u main\u elements()
,但我还必须确保只调用了一次
define('ONLY_ONCE_ap3_divi_do_shortcodes', true);
这就是我的结局
<?php
/**
* Render the Divi shortcode in wp-json API
* Placed in the wp-content/mu-plugins/ folder
*/
add_action( 'rest_api_init', function () {
register_rest_field(
'post',
'content',
array(
'get_callback' => 'ap3_divi_do_shortcodes',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
'post',
'excerpt',
array(
'get_callback' => 'ap3_divi_do_shortcodes',
'update_callback' => null,
'schema' => null,
)
);
});
function ap3_divi_do_shortcodes( $object, $field_name, $request ) {
// Set is_singular to true to avoid "read more issue"
// Issue come from is_singular () in divi-builder.php line 73
global $wp_query;
$wp_query->is_singular = true;
// Set divi shortcode
// The 2 function below are define in 'init' but they are call in 'wp'
// REST Api exit after 'parse_request' hook, it's before 'wp' so divi's shortcode are not set
if( !defined('ONLY_ONCE_ap3_divi_do_shortcodes') && function_exists('et_builder_init_global_settings') && function_exists('et_builder_add_main_elements') ) {
define('ONLY_ONCE_ap3_divi_do_shortcodes', true);
et_builder_add_main_elements();
}
//Define $post, if not defined, divi will not add outter_content and inner_content warper
//Issue come from get_the_ID() in divi-builder.php line 69
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;
}
我自己想出来的。我需要调用et\u builder\u add\u main\u elements()
,但我还必须确保只调用了一次
define('ONLY_ONCE_ap3_divi_do_shortcodes', true);
这就是我的结局
<?php
/**
* Render the Divi shortcode in wp-json API
* Placed in the wp-content/mu-plugins/ folder
*/
add_action( 'rest_api_init', function () {
register_rest_field(
'post',
'content',
array(
'get_callback' => 'ap3_divi_do_shortcodes',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
'post',
'excerpt',
array(
'get_callback' => 'ap3_divi_do_shortcodes',
'update_callback' => null,
'schema' => null,
)
);
});
function ap3_divi_do_shortcodes( $object, $field_name, $request ) {
// Set is_singular to true to avoid "read more issue"
// Issue come from is_singular () in divi-builder.php line 73
global $wp_query;
$wp_query->is_singular = true;
// Set divi shortcode
// The 2 function below are define in 'init' but they are call in 'wp'
// REST Api exit after 'parse_request' hook, it's before 'wp' so divi's shortcode are not set
if( !defined('ONLY_ONCE_ap3_divi_do_shortcodes') && function_exists('et_builder_init_global_settings') && function_exists('et_builder_add_main_elements') ) {
define('ONLY_ONCE_ap3_divi_do_shortcodes', true);
et_builder_add_main_elements();
}
//Define $post, if not defined, divi will not add outter_content and inner_content warper
//Issue come from get_the_ID() in divi-builder.php line 69
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;
}
这篇文章是救命稻草。谢谢。这篇文章是救命稻草。谢谢。你在为Divi构建GatsbyJS无头前端吗?我在使用AppPresser插件。你在为Divi构建GatsbyJS无头前端吗?我在使用AppPresser插件。