【发布时间】:2014-09-13 18:48:44
【问题描述】:
我正在为 wordpress 使用“json-API”插件,我正在尝试将该信息调用到 phonegap 应用程序中。
我正在关注http://alexbachuk.com/wordpress-and-phonegap-part3/ 上的帖子,并试图找出如何在我的内容中调用 custom_fields。
我已经像这样包含了自定义字段: ajax 请求中的“http://www.example.com/?json=get_recent_posts&custom_fields=store-icon”。
ajax 请求如下所示:
product: function(){
function getProducts() {
var dfd = $.Deferred();
$.ajax({
url: 'http://delectable.site40.net/blog/?json=get_recent_posts&custom_fields=store-icon',
type: 'GET',
dataType: 'json',
success: function(data){
var source = $("#product-template").html();
var template = Handlebars.compile(source);
var blogData = template(data);
$('#product-data').html(blogData);
$('#product-data').trigger('create');
dfd.resolve(data);
},
error: function(data){
console.log(data);
}
});
return dfd.promise();
};
getProducts().then(function(data){
$('#all-posts').on('click','li', function(e){
localStorage.setItem('postData', JSON.stringify(data.posts[$(this).index()]));
});
});
}
模板目前看起来是这样的:
<script id="product-template" type="text/x-handlebars-template">
<ul data-role="listview" data-icon="false" class="mainContent" data-theme="a" id="all-posts">
{{#each posts}}
<li class="center productss"><p class="photo circle center" style="margin-left: 31%;"><img src="{{thumbnail}}" width="85" height="57" /></ br><a data-ajax="false" data-transition="slide" href="single.html?{{@index}}"><h3 class="main_product">{{title}}</h3></a></ br><h5 class="left">R4200-00</h5><h5 class="right"><img src="{{custom_fields[0].url}}" width="150" height="20" /></h5></p></li>
{{/each}}
</ul>
</script>
我将如何将其插入到我的 html 中。 alexbachuk.com 帖子使用把手解析 json,因此帖子标题输出为 {{title}},缩略图输出为 {{thumbnail}}。有没有办法以类似的方式输出custom_fields?
【问题讨论】:
标签: wordpress cordova custom-fields handlebars.js json-api