未显示特色图像并出现错误“OBJECT OBJECT of CLASS WP_ERROR CATO NOT TO CONVERT TO INT”

时间:2013-09-30 作者:usama sulaiman

首先,我使用了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

我如何解决这个问题,为什么只有第一个表单没有设置特征图像,即使它将图像附加到帖子上

2 个回复
SO网友:usama sulaiman

最后我解决了这个问题

我一直在跟踪代码中的每个地方,并开始一步一步地注释代码部分。

正如我在问题中所说,我有一个表格upload the image (using upload form and also using image from link), 这是主要问题a conflict between uploading using upload form and using image link

因此,当我对用于从PC上传图像的函数进行注释时,代码工作得非常好

SO网友:s_ha_dum

您的一个函数调用正在返回WP_Error 对象,而您正试图将该对象当作整数使用。这可能是罪魁祸首:

$pid = wp_insert_post($new_post);
我说不出为什么,因为代码脱离上下文,而您没有包括$new_post.

直接包含核心文件也可能给自己带来麻烦:

require_once(ABSPATH . \'wp-admin/includes/image.php\'); 
该文件可能需要其他未正确加载的文件。

结束

相关推荐