简体   繁体   中英

Upload images gallery to wordpress with python-wordpress-xmlrpc

Okay I have created a script in python using "python-wordpress-xmlrpc" package I am able to do everything except for uploading multiple images to wordpress and then adding them to gallery.

Here is my code to upload a single image file and then set as feature image:

fileImg = urlopen('image_url')
imageName = fileImg.url.split('/')[-1]
imageType = mimetypes.guess_type(str(fileImg.url))[0]

data = {
    'name': imageName,
    'type': imageType,
}

data['bits'] = xmlrpc_client.Binary(fileImg.read())

response = client.call(media.UploadFile(data))
attachment_id = response['id']
widget.thumbnail = attachment_id

Note: I know how to upload multiple files but i don't understand how to add those images in Product gallery.

This technique worked for me:

attachment_id = response['id']
alt_value = "image alt"

sql = """INSERT INTO wp_postmeta (post_id,meta_key,meta_value) VALUES (%d,'_wp_attachment_image_alt','%s');""" % (int(attachment_id ), alt_value)
mycursor.execute(sql)
mydb.commit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM