我正在使用WordPress作为后端开发离子应用程序。
为此,我使用wp_query从WordPress获取博客文章,并将其显示在ionic应用程序中。
这里的问题是,帖子内容未解析为离子形式的HTML。但是在WordPress编辑器和博客网站的前端,它可以正确呈现。请在下面的屏幕截图和代码中找到
WordPress中的Rest API代码
$posts_array = array();
$args = array("post_type" => "post", "orderby" => "date", "order" => "DESC", "post_status" => "publish", "posts_per_page" => "-1");
$posts = new WP_Query($args);
if($posts->have_posts()):
while($posts->have_posts()):
$posts->the_post();
$post_array = array(
'title'=>get_the_title(),
'permalink'=>get_the_permalink(),
'content'=>get_the_content(),
'date'=>get_the_date(),
'img'=>wp_get_attachment_url(get_post_thumbnail_id($post->ID), 'thumbnail')
);
array_push($posts_array, $post_array);
endwhile;
else:
echo "{'posts' = []}";
die();
endif;
echo json_encode($posts_array);
离子应用中的HTML
<section class="list" ng-controller="postController">
<a href="#" class="item" ng-repeat="post in postsList">
<article>
<img ng-src="{{post.img}}" class="item">
<h2 class="noWhitespace">{{post.title}}</h2>
<p class="noWhitespace">{{post.content}}</p> <!-- ng-bind-html="toTrustedHTML(post.content)"-->
</article>
</a>
</section>
控制者
app.controller("postController",function ($scope, $http){ //, $sce
$scope.postsList = [];
$http({
method: "GET",
url: "http://localhost/wordpress/blog-posts/",
}).then(function(response){
console.log(response)
$scope.postsList = response.data;
});
/*$scope.toTrustedHTML = function (RESOURCE_URL) {
return $sce.trustAsHtml(RESOURCE_URL);
};*/
});
甚至我也尝试了ng-sanitize和$ sce进行角度角度的HTML解析。
在这里
ng-bind-html="toTrustedHTML(html)"
是有效的,但问题再次在于,段落之间没有换行符。 (显示长文字)提前致谢。
注意:
我们有Rest-WP API和WordPress的JSON-API插件来实现此目的。
但我需要在没有插件的情况下实现这一目标。
于2016年12月25日更新
如何将数据传递到另一个控制器?
在
'postController'
中,我们获得了response.data,其中包含所有帖子详细信息,如果单击单个帖子,则如何获取相应帖子的ID,标题和内容。我尝试使用$state.go()
,但未定义。提前致谢
最佳答案
在此处为可能会发现此问题的其他用户添加解决方案:
使用ng-bind-html使Angular显示html并通过wpautop()函数从数据库运行内容以获取正确的格式
关于javascript - 没有插件的WordPress REST API使用wp_query,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41299939/