关于 wordpress-content
通过 WP-CLI 或 REST API 管理 WordPress 文章、页面、媒体、分类、标签、菜单等内容,并涵盖验证、批量操作及导入导出。
主要功能
- 01创建和更新文章与页面
- 02上传和管理媒体
- 03管理分类、标签和菜单
- 04WP-CLI 不可用时使用 REST API
- 05处理内容搜索、导入和导出
适用场景
- 01发布或安排 WordPress 文章
- 02批量更新网站媒体和分类体系
- 03批量导入或导出网站内容
- 04通过 REST API 在无 SSH 时管理内容
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ jezweb/ claude-skills --skill wordpress-content安装后在 Claude Code 中输入/wordpress-content确认已加载。
详细介绍
WordPress 内容
创建、更新和管理 WordPress 内容——文章、页面、媒体、分类、标签和菜单。通过 WP-CLI 或 REST API 在网站上发布实时内容。
前置条件
- 可用的 WP-CLI SSH 连接或 REST API 凭据(使用 wordpress-setup 技能)
- 来自
wordpress.config.json或wp-cli.yml的站点配置
工作流
步骤 1:确定操作
| 任务 | 最佳方法 |
|---|---|
| 创建/编辑单篇文章或页面 | WP-CLI wp post create/update |
| 批量创建文章 | WP-CLI 循环或 REST API 批处理 |
| 上传图像/媒体 | WP-CLI wp media import |
| 管理分类/标签 | WP-CLI wp term |
| 更新导航菜单 | WP-CLI wp menu |
| 定时发布文章 | 使用 --post_date 的 WP-CLI |
| 复杂 HTML 内容 | 写入临时文件,再传递给 WP-CLI |
| 无法使用 SSH | 使用应用程序密码通过 REST API 操作 |
步骤 2:创建内容
博客文章
# Simple post
wp @site post create \
--post_type=post \
--post_title="My New Blog Post" \
--post_content="<p>Post content here.</p>" \
--post_status=draft \
--post_category=3,5
# Post from HTML file (better for long content)
wp @site post create ./post-content.html \
--post_type=post \
--post_title="My New Blog Post" \
--post_status=draft \
--post_excerpt="A brief summary of the post." \
--post_category=3,5 \
--tags_input="tag1,tag2"
文章状态:draft、publish、pending、future(与 --post_date 搭配使用)
页面
wp @site post create \
--post_type=page \
--post_title="About Us" \
--post_content="<h2>Our Story</h2><p>Content here...</p>" \
--post_status=publish \
--post_parent=0 \
--menu_order=10
定时发布的文章
wp @site post create \
--post_type=post \
--post_title="Scheduled Post" \
--post_content="<p>This goes live tomorrow.</p>" \
--post_status=future \
--post_date="2026-02-23 09:00:00"
步骤 3:上传媒体
# Upload from URL
wp @site media import "https://example.com/image.jpg" \
--title="Product Photo" \
--alt="Product front view" \
--caption="Our latest product"
# Upload from local file (requires SCP first for remote sites)
scp ./image.jpg user@host:/tmp/image.jpg
wp @site media import /tmp/image.jpg --title="Local Upload"
# Import and set as featured image in one step
wp @site media import "https://example.com/hero.jpg" \
--title="Hero" --featured_image --post_id={id}
# List media
wp @site post list --post_type=attachment --fields=ID,post_title,guid
# Regenerate thumbnails
wp @site media regenerate --yes
为文章设置特色图片:
# Get the attachment ID from the import output, then:
wp @site post meta update {post_id} _thumbnail_id {attachment_id}
步骤 4:管理分类法
分类
# List categories
wp @site term list category --fields=term_id,name,slug,count
# Create category
wp @site term create category "News" --slug=news --description="Company news and updates"
# Create child category
wp @site term create category "Product News" --slug=product-news --parent=5
# Update category
wp @site term update category {term_id} --name="Updated Name"
# Assign category to post
wp @site post term add {post_id} category news
标签
# List tags
wp @site term list post_tag --fields=term_id,name,slug,count
# Create tag
wp @site term create post_tag "new-tag"
# Add tags during post creation
wp @site post create --post_title="..." --tags_input="seo,marketing,tips"
# Add tags to existing post
wp @site post term add {post_id} post_tag seo marketing tips
步骤 5:管理菜单
# List menus
wp @site menu list --fields=term_id,name,slug,count
# List items in a menu
wp @site menu item list main-menu --fields=db_id,type,title,link,position
# Add page to menu
wp @site menu item add-post main-menu {page_id} --title="About Us"
# Add custom link
wp @site menu item add-custom main-menu "Contact" "https://example.com/contact/"
# Add category archive to menu
wp @site menu item add-term main-menu category {term_id}
# Reorder (set position)
wp @site menu item update {item_id} --position=3
# Delete menu item
wp @site menu item delete {item_id}
步骤 6:更新现有内容
# Update post title and content
wp @site post update {post_id} \
--post_title="Updated Title" \
--post_content="<p>New content.</p>"
# Update from file
wp @site post update {post_id} ./updated-content.html
# Search posts
wp @site post list --s="search term" --fields=ID,post_title
# Bulk update status
wp @site post list --post_type=post --post_status=draft --field=ID | \
xargs -I {} wp @site post update {} --post_status=publish
# Delete (trash)
wp @site post delete {post_id}
# Delete permanently
wp @site post delete {post_id} --force
步骤 7:文章元数据和自定义字段
# Get all meta for a post
wp @site post meta list {post_id} --fields=meta_key,meta_value
# Get specific meta
wp @site post meta get {post_id} meta_key
# Set meta
wp @site post meta update {post_id} meta_key "meta_value"
# Add meta (allows duplicates)
wp @site post meta add {post_id} meta_key "meta_value"
# Delete meta
wp @site post meta delete {post_id} meta_key
ACF 会同时存储字段值和引用键(_field_name -> field_abc123)。
步骤 8:搜索和替换
# Dry run first — always
wp @site search-replace "old text" "new text" --dry-run
# Execute
wp @site search-replace "old text" "new text" --precise
# Limit to specific table
wp @site search-replace "old" "new" wp_posts --precise
# Limit to specific column
wp @site search-replace "old" "new" wp_posts post_content --precise
步骤 9:导出和导入
# Export all content
wp @site export --dir=/tmp/
# Export specific post type
wp @site export --post_type=post --dir=/tmp/
# Import
wp @site import /path/to/file.xml --authors=mapping.csv
步骤 10:验证
# Check the post
wp @site post get {post_id} --fields=ID,post_title,post_status,guid
# Get the live URL
wp @site post get {post_id} --field=guid
# List recent posts
wp @site post list --post_type=post --posts_per_page=5 --fields=ID,post_title,post_status,post_date
提供管理后台 URL 和实时页面 URL:
- 管理后台:
https://example.com/wp-admin/post.php?post={id}&action=edit - 实时页面:
https://example.com/{slug}/
REST API 参考
无法使用 WP-CLI 时,请使用通过应用程序密码进行身份验证的 WordPress REST API。
身份验证
# Base64 encode credentials
AUTH=$(echo -n "username:xxxx xxxx xxxx xxxx xxxx xxxx" | base64)
# Use in requests
curl -s https://example.com/wp-json/wp/v2/posts \
-H "Authorization: Basic $AUTH" \
-H "Content-Type: application/json"
端点
| 资源 | 端点 |
|---|---|
| 文章 | /wp-json/wp/v2/posts |
| 页面 | /wp-json/wp/v2/pages |
| 媒体 | /wp-json/wp/v2/media |
| 分类 | /wp-json/wp/v2/categories |
| 标签 | /wp-json/wp/v2/tags |
所有端点均支持 GET(列表/单项)、POST(创建)、PUT(更新)、DELETE。
通过 REST 创建文章
curl -s https://example.com/wp-json/wp/v2/posts \
-H "Authorization: Basic $AUTH" \
-H "Content-Type: application/json" \
-d '{
"title": "My New Post",
"content": "<p>Post content here.</p>",
"status": "draft",
"categories": [3, 5],
"tags": [10, 12],
"excerpt": "Brief summary",
"featured_media": 456
}' | jq '{id, link, status}'
通过 REST 创建页面
curl -s https://example.com/wp-json/wp/v2/pages \
-H "Authorization: Basic $AUTH" \
-H "Content-Type: application/json" \
-d '{
"title": "About Us",
"content": "<h2>Our Story</h2><p>Content...</p>",
"status": "publish",
"parent": 0
}'
通过 REST 上传媒体
curl -s https://example.com/wp-json/wp/v2/media \
-H "Authorization: Basic $AUTH" \
-H "Content-Disposition: attachment; filename=photo.jpg" \
-H "Content-Type: image/jpeg" \
--data-binary @photo.jpg | jq '{id, source_url}'
通过 REST 创建分类
curl -s https://example.com/wp-json/wp/v2/categories \
-H "Authorization: Basic $AUTH" \
-H "Content-Type: application/json" \
-d '{"name": "News", "slug": "news", "description": "Company updates"}'
查询参数
| 参数 | 用途 | 示例 |
|---|---|---|
per_page | 每页结果数(最多 100) | ?per_page=50 |
page | 分页 | ?page=2 |
search | 搜索词 | ?search=keyword |
status | 按状态筛选 | ?status=draft |
categories | 按分类 ID 筛选 | ?categories=3 |
orderby | 排序字段 | ?orderby=date |
order | 排序方向 | ?order=desc |
_fields | 限制响应字段 | ?_fields=id,title,link |
菜单:导航菜单对 REST API 的支持有限。/wp-json/wp/v2/navigation 端点适用于 FSE 主题中的区块式导航。对于经典菜单,请使用 WP-CLI。
关键模式
WP-CLI 中的 HTML 内容
对于超过一句话的内容,请将 HTML 写入临时文件并传递给命令:
cat > /tmp/post-content.html << 'EOF'
<h2>Section Heading</h2>
<p>Paragraph content with <strong>bold</strong> and <a href="/link">links</a>.</p>
<ul>
<li>List item one</li>
<li>List item two</li>
</ul>
EOF
wp @site post create /tmp/post-content.html --post_title="My Post" --post_status=draft
对于复杂 HTML,--post_content 中的 Shell 引号容易出错。
批量操作
要创建多篇文章,请使用循环并验证结果:
while IFS=, read -r title slug content_file category; do
wp @site post create "$content_file" \
--post_type=post \
--post_title="$title" \
--post_name="$slug" \
--post_category="$category" \
--post_status=draft
sleep 0.5
done < posts.csv
始终先创建为 draft,审核后再批量发布。
评论
支持 Markdown