我想从 WordPress DB 中获取数据,将它们推送到数组中并将数据传递给 JS。
除了将数据推入foreach ($posts as $post)
.
完整方法:
$array = array();
$category_name = "locations";
$args = array(
'numberposts' => -1,
'category_name' => $category_name
);
$posts = get_posts($args);
foreach ($posts as $post){
array_push( $array, array(
"title" => the_title($post->ID),
"cnt" => the_content($post->ID),
"id" => the_ID($post->ID),
"link" => the_permalink($post->ID)
));
}
当我这样做时,print_r($array)
我得到以下组合:
Post 39http://localhost:8080/testing/post-3/Post 27http://localhost:8080/testing/post-2/Post 15http://localhost:8080/testing/post-1/Array
(
[0] => Array
(
[title] =>
[cnt] =>
[id] =>
[link] =>
)
[1] => Array
(
[title] =>
[cnt] =>
[id] =>
[link] =>
)
[2] => Array
(
[title] =>
[cnt] =>
[id] =>
[link] =>
)
)
到底是怎么回事?为什么数据没有正确放入数组?
任何建议都非常感谢。