2016-12-01 67 views
0

我正在開發一個網站,用戶可以通過它更新爲發佈縮略圖來捕獲他/她的圖片(通過.getusermedia)。爲特色圖片添加用戶元

的問題是,後縮略圖獲取所有用戶更新 - 我想後縮略圖,只對特定用戶

function Generate_Featured_Image($filename, $parent_post_id ){ 
    require('/wp-load.php'); 

    $filetype = wp_check_filetype(basename($filename), null); 

    $wp_upload_dir = wp_upload_dir(); 

    $attachment = array(
     'guid'   => $wp_upload_dir['url'] . '/' . basename( $filename), 
     'post_mime_type' => $filetype['type'], 
     'post_title'  => preg_replace('/\.[^.]+$/', '', basename( $filename)), 
     'post_content' => '', 
     'post_status' => 'inherit' 
    ); 

    $attach_id = wp_insert_attachment($attachment, $filename, $parent_post_id); 

    require_once(ABSPATH . 'wp-admin/includes/image.php'); 

    $attach_data = wp_generate_attachment_metadata($attach_id, $filename); 
    wp_update_attachment_metadata($attach_id, $attach_data); 

    //add_user_meta($current_user_id, $parent_post_id, $attach_id); 
    set_post_thumbnail($parent_post_id, $attach_id); 
} 

的script.php

Generate_Featured_Image($addroot.$current_user_id.$extimage, 88 ); 
// addroot=path ext-extension(.jpg) (this is name of file saved) 

我試圖更新用戶add_user_meta完成任務,但甚至無法啓動

UPDATE

<?php 
// $filename is succesfully saved as currentuserid+.jpg. 
$addroot = '/wp-content/uploads/2016/09/'; 


    $current_user_id = get_current_user_id(); 


$extimage = '.jpg'; 


$filename = $addroot.$current_user_id.$extimage; 

$filetype = wp_check_filetype(basename($filename), null); 

// Get the path to the upload directory. 
$wp_upload_dir = wp_upload_dir(); 

// Prepare an array of post data for the attachment. 
$attachment = array(
'guid'   => $wp_upload_dir['url'] . '/' . basename($filename), 
'post_mime_type' => $filetype['type'], 
'post_title'  => preg_replace('/\.[^.]+$/', '', basename($filename)), 
'post_content' => '', 
'post_status' => 'inherit' 
); 

// Insert the attachment. 
$attach_id = wp_insert_attachment($attachment, $filename, 0); 

require_once(ABSPATH . 'wp-admin/includes/image.php'); 

    // Generate the metadata for the attachment, and update the database record. 
    $attach_data = wp_generate_attachment_metadata($attach_id, $filename); 
     wp_update_attachment_metadata($attach_id, $attach_data); 
    update_user_meta(get_the_ID(), $filename , $_POST[ $filename ]); 
    ?> 

如何從usermeta由$ attach_id調用圖像進一步進行,並displayinf用戶爲thumbnail-摸不清這一步

+0

是POS噸縮略圖應該更新一個和每個用戶相同?爲什麼將它設置爲發佈縮略圖而不是某個用戶元字段? –

+0

是的帖子是相同的,即所有用戶的帖子ID相同。我只是想爲不同的用戶提供不同的特色圖片(由他們上傳) –

回答

1

你真的不能設定職位的不同特色圖片基於用戶。只有一篇文章,因此只有一篇有特色的圖片。
如果你想顯示一個不同的圖像,根據哪個用戶正在查看帖子,保存使用wp_insert_attachment($attachment, $filename, 0)插入附件,而不綁定到帖子。然後將該$attach_id保存到usermeta表。然後,當用戶查看該帖子時,只需獲取$attach_idget_user_meta並顯示該圖片(例如,您可以使用wp_get_attachment_url),而不是帖子的精選圖片。

UPDATE
首先,節省用戶的元數據應該是這樣的

update_user_meta($current_user_id, '_avatar_id', $attach_id); 

其次,在開始的時候,你應該檢查,如果用戶使用is_user_logged_in功能登錄。

第三,你應該檢查用戶不得不化身之前並刪除它(我的意思是,爲什麼存儲他們的老後,他們有一個新的,對吧?),就像這樣:

$old_attach=get_user_meta($current_user_id, '_avatar_id', true); 
if(is_numeric($old_attach)) 
{ 
    wp_delete_attachment($old_attach, true); 
} 

您的最終代碼保存形象應該是這樣的:

if (is_user_logged_in()) 
{ 
    $addroot = '/wp-content/uploads/2016/09/'; 
    $current_user_id = get_current_user_id(); 
    $extimage = '.jpg'; 
    $filename = $addroot . $current_user_id . $extimage; 
    $filetype = wp_check_filetype(basename($filename), null); 
    $wp_upload_dir = wp_upload_dir(); 
    $attachment = array(
     'guid' => $wp_upload_dir['url'] . '/' . basename($filename), 
     'post_mime_type' => $filetype['type'], 
     'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)), 
     'post_content' => '', 
     'post_status' => 'inherit' 
    ); 
    $attach_id = wp_insert_attachment($attachment, $filename, 0); 
    require_once(ABSPATH . 'wp-admin/includes/image.php'); 
    wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $filename)); 
    $old_attach = get_user_meta($current_user_id, '_avatar_id', true); 
    if (is_numeric($old_attach)) 
    { 
     wp_delete_attachment($old_attach, true); 
    } 
    update_user_meta($current_user_id, '_avatar_id', $attach_id); 
} 
else 
{ 
    //show error here or something 
} 

現在,訪問它,你可以寫一個簡單的功能,像這樣:

function get_user_avatar() 
{ 
    if (is_user_logged_in()) 
    { 
     $avatar_id = get_user_meta(get_current_user_id(), '_avatar_id', true); 
     if (is_numeric($avatar_id)) 
     { 
      return'<img src="' . wp_get_attachment_url($avatar_id) . '" alt="User avatar"/>'; 
     } 
     else 
     { 
      return '<img src="url_to_your_default_avatar" alt="User avatar"/>'; 
     } 
    } 
    return false; 
} 
+0

將圖片綁定到帖子上非常重要,一共有700個帖子。 –

+0

@AviSangray好,就像我說過的,你不能爲用戶的相同帖子綁定不同的圖片。也許你需要用這個重新思考你的邏輯。 –

+0

我明白你的意思了。也許我應該設置爲上傳後爲每個用戶創建一個有限可見性的新帖子。 –