用于将本地或远程文件导入WordPress媒体库,支持批量导入和附件管理。
适合需要批量上传图片、设置特色图像或从URL导入媒体文件的场景。
| 参数 | 说明 |
|---|---|
<file>... |
要导入的文件路径,支持当前shell的glob(3)通配符。如果文件被识别为URL(例如带有http或ftp协议),文件将在侧载前下载到临时文件。 |
[--post_id=<post_id>] |
要将导入文件附加到的文章ID。 |
[--post_name=<post_name>] |
要附加导入文件的文章名称。 |
[--file_name=<name>] |
附件名称(post_name字段)。 |
[--title=<title>] |
附件标题(文章标题字段)。 |
[--caption=<caption>] |
附件说明(文章摘要字段)。 |
[--alt=<alt_text>] |
图片的alt文本(保存为文章元数据)。 |
[--desc=<description>] |
附件文章的”描述”字段(文章内容)。 |
[--skip-copy] |
如果设置,媒体文件(仅本地)导入到库但不在磁盘上移动。设置此选项后,文件名不会经过wp_unique_filename()处理。 |
[--preserve-filetime] |
使用文件修改时间作为文章发布和修改日期。远程文件将始终使用当前时间。 |
[--featured_image] |
如果设置,将导入的图像设置为所附加文章的特色图像。 |
[--porcelain[=<field>]] |
输出每个导入图像的单个字段。用作标志时默认为附件ID。选项:url |
$ wp media import ~/Pictures/**/*.jpg
Imported file '/home/person/Pictures/landscape-photo.jpg' as attachment ID 1751.
Imported file '/home/person/Pictures/fashion-icon.jpg' as attachment ID 1752.
Success: Imported 2 of 2 items.
$ wp media import ~/Downloads/image.png --post_id=123 --title="下载的图片" --featured_image
Imported file '/home/person/Downloads/image.png' as attachment ID 1753 and attached to post 123 as featured image.
Success: Imported 1 of 1 images.
$ ATTACHMENT_ID="$(wp media import ~/Downloads/image.png --porcelain)"
$ wp post list --post_type=post --format=ids | xargs -d ' ' -I % wp post meta add % _thumbnail_id $ATTACHMENT_ID
Success: Added custom field.
Success: Added custom field.
$ wp media import https://s.w.org/style/images/wp-header-logo.png --title='WordPress标志' --alt="Semantic personal publishing"
Imported file 'https://s.w.org/style/images/wp-header-logo.png' as attachment ID 1755.
Success: Imported 1 of 1 images.
$ wp media import https://s.w.org/style/images/wp-header-logo.png --porcelain | xargs -I {} wp post list --post__in={} --field=url --post_type=attachment
http://wordpress-develop.dev/wp-header-logo/