我想添加到foreach数组中以包含精选图片网址和固定链接。
我该怎么做?
<?php
$posts_array = get_posts();
$send_array = array();
foreach ($posts_array as $key => $value) {
$send_array[$key]['ID'] = $value->ID;
$send_array[$key]['post_title'] = $value->post_title;
$send_array[$key]['post_content'] = $value->post_content;
};
?>
<script>var getPost = <?php echo json_encode( $posts_array ); ?>;</script>
答案 0 :(得分:0)
<强>更新强>
试试这个
<?php
$posts_array = get_posts();
$send_array = array();
foreach ($posts_array as $_post) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $_post->ID );
$send_array[] = array(
'id' => $_post->ID;
'post_title' => $_post->post_title,
'post_content' => $_post->post_content,
'permalink' => get_permalink($_post->ID),
'thumbnail' => $image[0]
);
};
?>
<script>var getPost = <?php echo json_encode( $send_array ); ?>;</script>