简体   繁体   English

WP REST API和React Native:如何不以HTML显示内容

[英]WP REST API and React Native: how to show content not in HTML

Simple (i'm sarcastic) question: is there any way to display CONTENT of wordpress post (that REST API returns as HTML) in a simple View or something? 简单的问题(我很讽刺):有没有办法在简单的View或类似的东西中显示wordpress post(REST API以HTML格式返回)的内容? With line-break, strong etc? 有换行,强等吗?

Something that i can do to change the API RESPONSE or to manipulate the result in React Native? 我可以做些什么来更改API响应或在React Native中操纵结果?

I've looked at WebView, https://github.com/archriss/react-native-render-html and a lot of other things. 我看过WebView, https://github.com/archriss/react-native-render-html和许多其他内容。

A possible work around is to make a custom endpoint to send your markup as a string, and then parse it on the client. 一种可能的解决方法是创建一个自定义终结点,以将标记作为字符串发送,然后在客户端上解析它。

Here is an example of an endpoint where I map my post titles to an array of h3's. 这是一个端点示例,其中我将帖子标题映射到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'
   ));
 });

I'm sure you could also change the response header to send markdown directly. 我确定您也可以更改响应标头以直接发送markdown。 Regardless hopefully this helps! 希望这会有所帮助!

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

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