如何在WordPress get_posts()json_encode()中包含精选图片网址?

时间:2016-12-13 23:35:08

标签: javascript php jquery wordpress

我想添加到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>

1 个答案:

答案 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>