您可以使用WP\\u Query从Wordpress安装中获取几乎任何post数据。
$sub_pages = new WP_Query(
array(
\'post_parent\' => 3,
\'post_type\' => \'page\'
)
);
print_r($sub_pages->posts);
这将为您带来以下好处:
Array
(
[0] => WP_Post Object
(
[ID] => 94
[post_author] => 1
[post_date] => 2015-08-20 11:19:27
[post_date_gmt] => 2015-08-20 10:19:27
[post_content] => Business operating...
满是帖子的数组。因此,您只需对其进行json编码,并将其包装在脚本标记中。
<script type="text/javascript">
var post_info = <?php echo json_encode($sub_pages->posts); ?>;
</script>
这将输出:
<script type="text/javascript">
var post_info = [{"ID":94,"post_author":"1","post_date":"2015-08-20 11:19:27","post_date_gmt":"2015-08-20 10:19:27","post_content":"Business operating
现在,如果我把它放在加载JavaScript的地方之前,即
<script type="text/javascript">
var post_info = <?php echo json_encode($sub_pages->posts); ?>;
</script>
<script type="text/javascript" src="script.js"></script>
在中
script.js
我可以走了
console.log(post_info[0].ID);
我们会得到
94
在控制台中。