插入提要中的特色图像

时间:2016-01-05 作者:Raja Gopal

我通过feed以编程方式导入产品,如下所示:

$request = wp_remote_get(\'myapiurl\');
$response = wp_remote_retrieve_body( $request );

// decode the json

$json = json_decode($response, true);

// loop through the results

if( ! empty($json[ \'results\' ])) {
    foreach($json[ \'results\' ] as $data) {

        // gather data you need, make sure to sanitize it

        $post_data = array(
              \'post_title\' => $data[ \'post_title\' ],
                \'post_name\' => $data[ \'post_name\' ],
                \'post_content\' => $data[ \'post_content\' ],
                \'post_category\' => $data[ \'post_category\' ],
                \'_price\' => $data[ \'price\' ],
                \'post_location\' => $data[ \'location\' ],
                \'post_stock\' => $data[ \'stock\' ],
                \'post_year\' => $data[ \'year\' ],
                \'post_images\' => $data[ \'images\' ],
                \'post_status\' => \'publish\',
                \'post_type\' => \'product\',
        );

        // create product or post and return the new ID

        $post_id = wp_insert_post($post_data, $wp_error);
    }
}
问题是图像没有作为特征图像插入。

请帮帮我

UPDATE :

Example: API Response (JSON)

{
    "status":"SUCCESS",
    "message":"Results have been extracted",
    "results":[{
                "post_title":"1984 AQUASPORT 222 FT W96 YAMAHA",
                "post_name":"1984-aquasport-22-2-ft-w96-ya",
                "post_content":"Nice boat 1984 aquasport 22.2 ft c.c 1996 yamaha 200 hp runs good.",
                "post_category":"By Owner",
                "price":"52000",
                "location":"Miami, FL",
                "stock":"952481",
                "year":"2007",
                "images":[
                          "http:\\/\\/boatexportusa.com\\/wp-content\\/plugins\\/api\\/get-image.php?src=fakesrc\\/00o0o_7TGyKJXFMp9_600x450.jpg",
                          "http:\\/\\/boatexportusa.com\\/wp-content\\/plugins\\/api\\/get-image.php?src=fakesrc\\/00o0o_7TGyKJXFMp9_600x450.jpg"
                         ],
                "cond":"new",
                "size":"27\'",
                "make":"SeaRay",
                "model":"S300",
                "hours":"330",
                "propulsion":"Power",
                "addedtime":"2015-03-23"
                }]
}

UPDATE 1 :

[post_images] => Array
(
[0] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00k0k_l1jwqDulnwk_600x450.jpg
[1] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00k0k_l1jwqDulnwk_600x450.jpg
[2] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00303_cALZYkakJx2_600x450.jpg
[3] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/01515_5nEeIcoBGot_600x450.jpg
)

UPDATE 2:

Echo for:“images”=>$数据[“images”]:

[images] => Array
(
[0] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00d0d_3Qnz0HTaTVP_600x450.jpg
[1] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00d0d_3Qnz0HTaTVP_600x450.jpg
[2] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00202_apLgMytA6Fo_600x450.jpg
[3] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00J0J_2rxcE4rqK3h_600x450.jpg
[4] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00t0t_4PKqbSme4FE_600x450.jpg
[5] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00q0q_jbVdZ1mNi9R_600x450.jpg
[6] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00r0r_l2XOi3b8tML_600x450.jpg
[7] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00j0j_cHeK38ceFIG_600x450.jpg
[8] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00D0D_dqcnZzF1Msl_600x450.jpg
[9] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00606_aWYjRYKgX0g_600x450.jpg
[10] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00707_dfM9vlGIvcS_600x450.jpg
[11] => http://boatexportusa.com/wp-content/plugins/api/get-image.php?src=fakesrc/00U0U_jINezkwXHhY_600x450.jpg
)
在代码中回声$image\\u id:

Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
)

1 个回复
SO网友:jgraup

您需要将图像放到服务器和库中。从那时起,你就可以把它们贴在柱子上。你只能1 featured image 因此,这需要进行调整,以设置功能一次,但仍然下载所有图像和attach them to the post.

我正在使用media_sideload_image 在本例中,但您可以使用wp_insert_attachment (该页上还有另一个示例)。

// the new post has been created
$post_id = wp_insert_post($post_data, $wp_error);

// now we\'ll deal with the images

$image_ids = array();

foreach($data[ \'images\' ] as $inx => $url) {

    // add external image to our library
    $src = media_sideload_image(rawurldecode($url), $post_id, NULL, \'src\');

    // convert to attachment src -> ID -> post
    $attach_id = attachment_url_to_postid($src);

    // grab the id so we can add to the post when we\'re done
    $image_ids[] = $attach_id;

    // access to attachment post
    // $attach_post = get_post($attach_id);

    // set the image\'s parent id to the post
    wp_update_post(array(\'ID\' => $attach_id, \'post_parent\' => $post_id));

    if($inx === 0) {
        // add the first image as the post\'s featured image
        add_post_meta($post_id, \'_thumbnail_id\', $attach_id, true);
    }
}

// add the ids onto the post\'s meta to use later -- store as simple string
add_post_meta($post_id, \'_images\', implode(\',\', $image_ids), true);
此代码尚未测试

<小时>post_images 不是的属性WP_Post 因此,它们需要以另一种方式进行关联。此外,许多其他属性也应使用add_post_meta 与其尝试添加帖子本身,不如看看custom fields.