我正在将 WP_Query
对象传递给 JavaScript 文件中的成功函数,但在尝试循环访问它时遇到问题。
我的 PHP:
$args = array(
'post_type' => 'post'
);
$query = new WP_Query( $args );
// Pass the $query object to the success function in my script.
echo json_encode( $query );
我的脚本中的成功函数:
success: function( data ) {
// I'd like to loop through the query object here.
},...
我知道如何在服务器端循环访问 WP_Query 对象:
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo get_the_title();
}
}
但是如何在脚本的成功函数中使用 jQuery 循环查询对象?
最佳答案
尝试像这样迭代它:
for(var i in data) {
if(data.hasOwnProperty(i)) {
console.log(data[i]);
}
}
关于javascript - 如何使用 jQuery 循环访问 WordPress WP_Query 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25691246/