我是Wordpress的新手,并使用WP REST API来获取要在另一个项目中使用的JSON数据。但是,在JSON数据中,我想进行一些调整,以便于使用这些数据。例如,在excerpt.rendered
部分中,我只想要纯文字,而不需要额外的<p>
或\n
或</p>
以及其他html标记。
我知道这可能与php文档有关,但我是WP新手,所以我需要对哪个文件进行一些更改以便我得到我想要的摘录?
答案 0 :(得分:0)
在将回复返回给其他项目之前,您是否只能使用strip_tags?
即
$excerpt = $json[excerpt][rendered];
return strip_tags($excerpt);
这应删除所有HTML标记和实体,并仅返回原始内容。
或者,如果您需要整个JSON响应,我想象在摘录序列化之前剥离标记应该有效。
尝试更换:
/**
* Check the post excerpt and prepare it for single post output.
*
* @param string $excerpt
* @return string|null $excerpt
*/
protected function prepare_excerpt_response( $excerpt ) {
if ( post_password_required() ) {
return __( 'There is no excerpt because this is a protected post.' );
}
/** This filter is documented in wp-includes/post-template.php */
$excerpt = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $excerpt ) );
if ( empty( $excerpt ) ) {
return '';
}
return $excerpt;
}
使用:
/**
* Check the post excerpt and prepare it for single post output.
*
* @param string $excerpt
* @return string|null $excerpt
*/
protected function prepare_excerpt_response( $excerpt ) {
if ( post_password_required() ) {
return __( 'There is no excerpt because this is a protected post.' );
}
/** This filter is documented in wp-includes/post-template.php */
$excerpt = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $excerpt ) );
if ( empty( $excerpt ) ) {
return '';
}
return strip_tags($excerpt);
}
class-wp-rest-posts-controller.php 第651行
答案 1 :(得分:0)
使用<WebView/>
来显示内容。 WebView使用隐藏的HTML标签显示内容。
答案 2 :(得分:0)
有一种相对简单的方法可以做到这一点。
要从摘录中删除环绕的 <p>
标记,请将以下行放入您的 functions.php 文件中:
remove_filter('the_excerpt', 'wpautop');
从内容中删除包装标签:
remove_filter ('the_content', 'wpautop');
更多详情:https://developer.wordpress.org/reference/functions/remove_filter/
如果您想删除其他自动添加的 HTML 标记,请查看是否有可以禁用的相关过滤器。否则,您必须更新 PHP 路由以手动删除有问题的 HTML。