Javascript WordPress REST API没有使用wp_查询的插件
我使用WordPress作为后端开发了一个ionic应用程序。 为此,我使用wp_查询从WordPress获取博客文章,并将其显示在ionic应用程序中 这里的问题是,post内容在ionic中没有解析为HTML。但是在WordPress编辑器和博客网站的前端,它的呈现是正确的。请在下面找到屏幕截图和代码 WordPress中的Rest API代码Javascript WordPress REST API没有使用wp_查询的插件,javascript,php,angularjs,wordpress,rest,Javascript,Php,Angularjs,Wordpress,Rest,我使用WordPress作为后端开发了一个ionic应用程序。 为此,我使用wp_查询从WordPress获取博客文章,并将其显示在ionic应用程序中 这里的问题是,post内容在ionic中没有解析为HTML。但是在WordPress编辑器和博客网站的前端,它的呈现是正确的。请在下面找到屏幕截图和代码 WordPress中的Rest API代码 $posts_array = array(); $args = array("post_type" => "post", "orderby"
$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>
甚至我也尝试了ng sanitize,并在angular中使用$sce进行HTML解析。
这里的ng bind html=“toTrustedHTML(html)”
工作正常,但问题是段落之间没有换行符。(显示长文本)
提前谢谢
注意:
我们有RESTWPAPI和用于WordPress的JSON-API插件来实现这一点。
但是我需要在没有插件的情况下实现这一点
于2016年12月25日更新
如何将数据传递到另一个控制器?
在'postController'
中,我们得到了response.data,它包含了所有帖子的详细信息,如果我们点击单个帖子,我们如何获得相应帖子的ID、标题和内容。我尝试使用$state.go()
但未定义
提前感谢为可能发现此问题的其他用户在此处添加解决方案:
使用ng bind html显示html,并通过wpautop()函数运行数据库中的内容,以获得正确的格式。通过将wpautop()函数添加到post数组中的内容,它得到了修复。工作代码如下所示:
$post_array = array(
'title'=>get_the_title(),
'permalink'=>get_the_permalink(),
'content'=>wpautop(get_the_content()),
'date'=>get_the_date(),
'img'=>wp_get_attachment_url(get_post_thumbnail_id($post->ID), 'thumbnail')
);
和控制器
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);
};
});
我们还在index.html中使用了ng绑定html
<article>
<img ng-src="{{post.img}}" class="item">
<h2 class="noWhitespace">{{post.title}}</h2>
<p class="noWhitespace" ng-bind-html="toTrustedHTML(post.content)"></p>
</article>
{{post.title}
感谢victor以正确的方式指导我。为什么不使用评论中的ng bind html?我尝试了ng bind html。它正在工作。。但是内容在段落之间没有换行符…这是因为wordpress有自己的方式来处理帖子中的新行使用wpautop()函数来获得所需的格式如何通过wpautop()函数运行数据库中的内容而不影响现有的wordpress功能?如果可能,请提供一个代码。如何将数据传递给另一个控制器?在
'postController'
中,我们得到了response.data,它包含了所有帖子的详细信息,如果我们点击单个帖子,我们如何获得相应帖子的ID、标题和内容。我尝试使用$state.go()
但未定义。。提前感谢如何将数据传递给另一个控制器?在“postController”中,我得到了response.data,其中包含了所有帖子的详细信息,如果我单击单个帖子,我如何获得相应帖子的ID、标题和内容。
<article>
<img ng-src="{{post.img}}" class="item">
<h2 class="noWhitespace">{{post.title}}</h2>
<p class="noWhitespace" ng-bind-html="toTrustedHTML(post.content)"></p>
</article>