我正在尝试制作一个插件,它可以从Instagram API获取图像,然后将它们放入短代码中,这样可以在短代码的位置设置特色图像。我有这个代码用于设置特色图像的功能:
<?php
function Generate_Featured_Image( $image_url, $post_id ){
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($image_url);
$filename = basename($image_url);
if(wp_mkdir_p($upload_dir['path'])) $file = $upload_dir['path'] . '/' . $filename;
else $file = $upload_dir['basedir'] . '/' . $filename;
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
$res1= wp_update_attachment_metadata( $attach_id, $attach_data );
$res2= set_post_thumbnail( $post_id, $attach_id );
}
然后我有了这个代码来获取API并将图像放在一个数组中:
//define Access token
$accesst= "3219594850.1677ed0.cb21f3e34b7c4030a8ba364d35ec284b";
//userid
$userid= 3219594850;
//image count to get
$count=20;
//get api contents
$content = file_get_contents('https://api.instagram.com/v1/users/self/media/recent/?access_token='.$accesst.'&count='.$count);
//converting JSON to object
$standardres = json_decode($content, true);
//array method
$images= array();
foreach($standardres['data'] as $photo) {
$imageData =file_get_contents($photo['images']['standard_resolution']['url']);
array_push ($images, $imageData);
}
这应该是使帖子成为特色图片的功能:
//create functions for shortcodes with definition
function fone(){
global $post;
$fid = $post->ID;
global $images;
Generate_Featured_Image($images[0], $fid);
}
add_shortcode( 'one', 'fone');
?>
但我只是显示错误:
Warning: file_get_contents() expects parameter 1 to be a valid path, string given in D:\XEMP\htdocs\xd\wordpress\wp-content\plugins\insta-live\insta-live.php on line 56
Warning: file_put_contents() expects parameter 1 to be a valid path, string given in D:\XEMP\htdocs\xd\wordpress\wp-content\plugins\insta-live\insta-live.php on line 60
...
任何想法我哪里出错了?第56和60行是:
$image_data = file_get_contents($image_url);
file_put_contents($file, $image_data);