首先,我使用了twentyeleven主题,创建了一个自定义模板页面来插入帖子。It has 4 submit forms. it is working well, also setting feature image is ok for 3 forms except the first one it is not setting feature image for the post even it upload the image (using upload form and also using image from link) and attach it to the post, 让我疯狂的是,我正在使用相同的函数来设置特征图像,但我不知道为什么它不适用于第一个提交表单
这是我设置特征图像的代码:
$pid = wp_insert_post($new_post);
$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, $pid );
require_once(ABSPATH . \'wp-admin/includes/image.php\');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $pid, $attach_id );
我收到了这些错误
1-在中home 第页和edit post 在管理区域:
Object of class WP_Error could not be converted to int in SITE_ROOT/public_html/wp-includes/post.php on line 4294
这让我想到wp_attachment_is_image
在主要岗位上的职能。php页面
function wp_attachment_is_image( $post_id = 0 ) {
$post_id = (int) $post_id;
if ( !$post = get_post( $post_id ) )
return false;
if ( !$file = get_attached_file( $post->ID ) )
return false;
$ext = preg_match(\'/\\.([^.]+)$/\', $file, $matches) ? strtolower($matches[1]) : false;
$image_exts = array( \'jpg\', \'jpeg\', \'jpe\', \'gif\', \'png\' );
if ( \'image/\' == substr($post->post_mime_type, 0, 6) || $ext && \'import\' == $post->post_mime_type && in_array($ext, $image_exts) )
return true;
return false;
}
我测试过
$post_id
by is\\u int和fount是整数
2-在主页中:Object of class WP_Error could not be converted to int in SITE_ROOT/public_html/wp-includes/functions.php on line 3814
这让我想到_get_non_cached_ids
在里面functions.php
页
function _get_non_cached_ids( $object_ids, $cache_key ) {
$clean = array();
foreach ( $object_ids as $id ) {
$id = (int) $id;
if ( !wp_cache_get( $id, $cache_key ) ) {
$clean[] = $id;
}
}
return $clean;
}
我测试过
$id
by is\\u int()我知道了
it is not integer我如何解决这个问题,为什么只有第一个表单没有设置特征图像,即使它将图像附加到帖子上