我想从Wordpress网站返回一个JSON数组。我知道这可以使用WP REST API和JSON API这样的插件来完成,但我正在处理自定义帖子类型,所以我想避免复杂性并自己编写查询。
我通过在我的主题目录中编写一个名为page-345.php的新php文件来覆盖我的一个页面。
在我的PHP文件中,我有以下代码
Component {
id:myDelegation
Item {
x: 50
width: 80
height: 60
Rectangle {
width: 60
height: 60
Text {
text: name
anchors.centerIn: parent
}
color: parent.ListView.isCurrentItem ? "red" : "steelblue";
scale: parent.ListView.isCurrentItem ? 1.5 : 1;
}
}
}
ListView {
id: listView1
x: 0
y: 50
width: 1920
height: 214
orientation: ListView.Horizontal
spacing: 4
model: TileList{}
delegate: myDelegation
preferredHighlightBegin: width / 2 - 10
preferredHighlightEnd: width / 2 + 10
highlightRangeMode: ListView.StrictlyEnforceRange
}
我编码工作,我得到JSON输出。但是,JSON in与模板的标题一起返回。 (见截图)
我也尝试输入代码
$args = array(
'post_type' => 'partner',
'post_status' => 'publish',
'posts_per_page' => -1 // all
);
$query = new WP_Query( $args );
$cars = array();
while( $query->have_posts() ) : $query->the_post();
// Add a car entry
$cars[] = array(
'name' => get_the_title()
);
endwhile;
wp_reset_query();
wp_send_json( $cars );
位于我的页面顶部,但随后Wordpress会返回一个错误消息,指出已经设置了标题。