2016-02-14 33 views
0

我的舊主題不支持Woocommerce,我目前正在將我的朋友網站升級到Woocommerce。 舊主題使用帖子顯示產品,並且我已成功將帖子類型更改爲產品以便在Woocommerce目錄中顯示。將內部郵件中的圖像移動到WooCommerce中的產品圖像

問題是她把產品圖像放在帖子裏面。有沒有辦法使用phpmyadmin將帖子內的圖片設置爲產品圖片?

回答

0

我創建了一個簡單的腳本來移動它們.. 工程與WordPress 4.4.2

// Product which doesn't have _thumbnail_id 
$sql = "SELECT ID FROM `25c4_posts` WHERE post_type = 'product' AND ID NOT IN (SELECT post_id FROM 25c4_postmeta WHERE meta_key = '_thumbnail_id')"; 
$result = $con->query($sql); 

while ($row = $result->fetch()) { 
    $post_id = $row['ID']; 
    $meta_key = '_thumbnail_id'; 

    // GET attachment_id. How? 
    $get_attachments_query = "SELECT ID FROM 25c4_posts WHERE post_parent = ".$post_id." AND post_type='attachment' LIMIT 1"; 
    $get_attachments = $con->query($get_attachments_query); 
    $attachments = $get_attachments->fetch(); 
    $attachment_id = $attachments[0]; 

    $stmt = $con->prepare('INSERT INTO 25c4_postmeta (post_id, meta_key, meta_value) VALUES (:post_id, :meta_key, :meta_value)'); 
    $stmt->bindParam(':post_id', $post_id); 
    $stmt->bindParam(':meta_key', $meta_key); 
    $stmt->bindParam(':meta_value', $attachment_id); 
    $stmt->execute(); 

    echo "<p>post_id: ".$post_id." | attachment_id: ".$attachment_id."<BR />".$get_attachments_query."</p>"; 
} 

PS:不要忘了在上面的腳本添加自己的數據庫連接

0

是的,有幾種方法。

首先,只需在帖子中查找附件標識(可能在db字段wp_posts-> post_content中),然後將其作爲產品圖像/縮略圖分配給產品。

爲此,您必須爲帖子ID添加帖子元條目(wp_post_meta),其中元字段爲「_thumbnail_id」,元值應該是您在帖子內容中找到的附件標識。

或者,你可以在wp-admin中的兩個步驟。

+0

謝謝,我明白了! –

相關問題