繁体   English   中英

Wordpress:从 WP REST API JSON 文件中删除不需要的 html 标签

[英]Wordpress: remove un-wanted html tag from WP REST API JSON file

我是 Wordpress 的新手,并使用 WP REST API 来获取要在另一个项目中使用的 JSON 数据。 但是,在 JSON 数据内部,我想进行一些调整,以便于使用这些数据。 例如,在excerpt.rendered部分,我只想要纯文字,没有额外的<p>\\n</p>和其他 html 标签。

在此处输入图片说明

我知道这可能与 php 文档有关,但我是 WP 的新手,所以我需要对哪个文件进行一些更改才能获得我想要的摘录?

有一种相对简单的方法可以做到这一点。

要从摘录中删除包装<p>标记,请将以下行放入您的 functions.php 文件中:

remove_filter('the_excerpt', 'wpautop');

要从内容中删除包装标签:

remove_filter ('the_content', 'wpautop');

更多详情: https : //developer.wordpress.org/reference/functions/remove_filter/

如果您想删除其他自动添加的 HTML 标记,请查看是否有可以禁用的相关过滤器。 否则,您将不得不更新您的 PHP 路由以手动删除有问题的 HTML。

使用<WebView/>显示内容。 WebView 显示带有隐藏 HTML 标记的内容。

在将您的回复返回给另一个项目之前,您不能只使用 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 行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM