【问题标题】:Upload images to media library based on custom field根据自定义字段上传图片到媒体库
【发布时间】:2013-05-14 11:03:53
【问题描述】:

我刚从一个定制的网站迁移过来。由于我不完全确定自己在做什么,我创建了一个名为 features_image 的自定义字段,并在其中为每个帖子放置了一个图片的 URL。

现在。

有没有一种方法可以循环浏览我的所有帖子并从自定义字段中的 URL 生成缩略图/特色图片?

希望这是有道理的!

更多信息。

我已经移动了 7000 个帖子,每个帖子都有一个自定义字段,其中包含一个指向图像的 URL。我想把这些网址做成相关帖子的特色图片。

我有一个插件可以拍摄帖子中的第一张图片并在我重新发布时,但这对于 7000 可能不实用!

谢谢

【问题讨论】:

  • Wordpress 已经开启了特色图片功能,您只需设置一下即可。

标签: wordpress curl upload media


【解决方案1】:

好的,我通过破解另一个插件找到了答案。

首先,我遍历 postmeta 表

$postid_list = $wpdb->get_results("SELECT distinct post_id FROM yars_postmeta WHERE meta_key='featured_image' ORDER BY post_id DESC LIMIT 10");
        if (!$postid_list){
                die('No posts with images were found.');
        }
        foreach ($postid_list as $v) {
            $post_id = $v->post_id;
            //$options['url_method'] = $url_method;
            echo fig_fetch_images($post_id).'<br/>';
        }

然后在一个函数中,我获取图像,然后将其上传到媒体库,并为帖子 ID 设置特色图像

function fig_fetch_images( $post_id ) { 
global $wpdb;
//Check to make sure function is not executed more than once on save

if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
return;

if ( !current_user_can('edit_post', $post_id) ) 
return;

remove_action('publish_post', 'fetch_images');  

//$post = get_post($post_id);   
$first_image = '';
$key = 'featured_image';
$first_image = get_post_meta($post_id, $key, true);
$wpdb->query("update yars_postmeta set meta_key ='featured_image_uploaded'WHERE meta_key='featured_image' AND post_id=".$post_id);

if (strpos($first_image,$_SERVER['HTTP_HOST'])===false) {

    //Fetch and Store the Image 

    $get = wp_remote_get( $first_image );

    $type = wp_remote_retrieve_header( $get, 'content-type' );

    $mirror = wp_upload_bits(rawurldecode(basename( $first_image )), '', wp_remote_retrieve_body( $get ) );


    //Attachment options

    $attachment = array(

    'post_title'=> basename( $first_image ),

    'post_mime_type' => $type

    );

    // Add the image to your media library and set as featured image

    $attach_id = wp_insert_attachment( $attachment, $mirror['file'], $post_id );

    $attach_data = wp_generate_attachment_metadata( $attach_id, $first_image );

    wp_update_attachment_metadata( $attach_id, $attach_data );

    set_post_thumbnail( $post_id, $attach_id );


    // re-hook this function

    add_action('publish_post', 'fetch_images');     

}
return ('Done post '. $post_id .' : '. $first_image);

}

原来的插件是Hotlink Image Cacher!

【讨论】:

  • 嘿,我也有同样的情况,我可以看到FROM yars_postmeta WHERE meta_key='featured_image' 这是什么yars_postmeta
  • Yars_postmeta 实际上对你来说会有所不同,当我发布这个时我没有意识到可以通过编程找到前缀。您的可能类似于数据库中的 wp_postmeta 。你得到前缀 $wpdb->prefix
【解决方案2】:

我认为你应该解释更多。在 single.php 的循环中,您是否有一个将自定义字段作为 HTML 图像标记返回的函数?如果是这样,有些插件会从帖子中的第一张图片或任何自定义帖子类型自动生成帖子缩略图(精选缩略图)。

其次,如果您的网站上仍然没有太多帖子,我认为添加文本自定义字段(包含图片网址)不是最好的方法,因为您可以创建负责保存图片的自定义字段,如果内置默认的特色图片字段是不够的。

但请先回答我们。您将在哪里以及如何使用缩略图? (使用代码解释更多)。最好能得到社区更多的帮助。

祝你好运

【讨论】:

  • Hi.Sadly 我无法用代码解释,因为我不知道该放什么!!我不是 PHP 大佬。但是,我添加了更多细节
猜你喜欢
  • 1970-01-01
  • 2012-01-18
  • 1970-01-01
  • 2016-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-09
  • 2016-03-02
相关资源
最近更新 更多