gpt4 book ai didi

android - WP REST API 和 React Native : how to show content not in HTML

转载 作者:行者123 更新时间:2023-11-30 05:05:10 25 4
gpt4 key购买 nike

简单(我很讽刺)的问题:有什么方法可以在简单的 View 或其他东西中显示 wordpress 帖子的内容(REST API 以 HTML 形式返回)?有换行,强等?

我可以做些什么来更改 API 响应或在 React Native 中操纵结果?

我查看了 WebView,https://github.com/archriss/react-native-render-html还有很多其他的东西。

最佳答案

一个可能的解决方法是制作一个自定义端点以将您的标记作为字符串发送,然后在客户端解析它。

这是一个端点示例,我在其中将我的帖子标题映射到 h3 的数组。

function postsAsHTML(){
$data = array();

$args = array(
'post_type' => 'post',
'posts_per_page' => '12',
);

$posts = new WP_Query($args);
?>

<?php if ($posts->have_posts()): ?>
<?php while ($posts->have_posts()) : $posts->the_post(); ?>
<?php
global $post;
$html = "
<div>
<h3>" . html_entity_decode(get_the_title()) . "</h3>
</div>
";
array_push($data, trim($html));
?>
<?php endwhile; ?>
<?php endif;

return array(
'data' => $data
);
}

// Register the rest route here.
add_action( 'rest_api_init', function () {
register_rest_route( 'YOUR_NAMESPACE/v1', 'posts-as-html',array(
'methods' => 'GET',
'callback' => 'postsAsHTML'
));
});

我相信您也可以更改响应 header 以直接发送 Markdown 。不管怎样,希望这会有所帮助!

关于android - WP REST API 和 React Native : how to show content not in HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54701243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com