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

问题描述 投票:2回答:2

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

enter image description here

我知道这可能与php文档有关,但我是WP新手,所以我需要做哪些更改文件以便我得到我想要的摘录?

php html json wordpress rest
2个回答
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行


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.