【问题标题】:How can I add a default image (or images) to my posts?如何将默认图像(或图像)添加到我的帖子中?
【发布时间】:2013-06-24 13:06:27
【问题描述】:

我目前正在制作一个 Wordpress 插件,并且几乎可以使用它。它是具有默认值、列和自定义字段(高级自定义字段)的自定义帖子类型。

function newplugin_install() {

$new_post = array( 
    'post_title' =>     'One Once Un Single',
    'post_content' =>   '(FIRST). Lorem ipsum dolor set elit...',
    'post_status' =>    'publish',
    'post_type' =>      'exhibitor'
    // INSERT default.png here somehow
);


//SAVE THE POST
$pid = wp_insert_post($new_post);


}

我想添加基于特色图像或基于 ACF 图像类型的默认图像。我不知道从哪里开始。

非常感谢,

【问题讨论】:

    标签: wordpress plugins custom-fields


    【解决方案1】:

    1-如果你需要插入帖子,首先

    注意:如果你知道post id并且不需要插入新的,你可以跳过第二步

    $new_post = array(
        'post_title'    =>  $title,
        'post_content'  =>  $outputValue,
        'post_category' =>  array($_POST['cat']),  // Usable for custom taxonomies too
        'tags_input'    =>   $tags_keywords,
        'post_status'   =>  'publish',           // Choose: publish, preview, future, draft, etc.
        'post_type' =>  'post'  //'post',page' or use a custom post type if you want to
        );
    
        //SAVE THE POST
      $pid = wp_insert_post($new_post);
    

    2- 秒插入图片

    // 代码:如果你想从 URL 插入图片

    if(isset($_POST['the_img_link']) && $_POST['the_img_link'] != ''){
        $image_url = $_POST['the_img_link'];
        basename($image_url);
        $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 );
    }else{
        // $other_attach_id is any previous added attached image
        // you want it to be a default image
        set_post_thumbnail( $pid, $other_attach_id )
    }
    

    // 代码:如果你想在你的电脑上插入图片

    if ($_FILES) {
        foreach ($_FILES as $file => $array) {
            $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 );
        }
    }else{
        // $other_attach_id is any previous added attached image
        // you want it to be a default image
        set_post_thumbnail( $pid, $other_attach_id )
    }
    

    【讨论】:

    • 最好在自定义模板页面中进行,您可以按照该教程进行操作Wordpress Codex Page_Templates
    • 那么这应该在我的示例自定义帖子类型中插入图像吗?您的代码从哪里获取默认图像?
    • 是的,这将为您的帖子添加特色图片,我编辑了最后一段代码,因此它可以很好地与您合作,因为我在我的 Functions.php 文件中进行了一些编辑,所以您不要不需要编辑那个文件
    • 我已经添加了你的代码,但它似乎没有做任何事情。我仍然看不到默认文件的来源。我输入了一些默认内容,但我不知道如何查看我的 default.png 文件。
    • $_POST['the_img_link'] 对我不起作用我没有通过某种形式插入内容。我在安装自定义帖子类型插件后插入内容(我的图像)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多