🛠 工具 / 开源
无需插件:用 WP CLI 查找缺失特色图片和 alt 文本的博客文章Find blog posts with missing featured images - and missing alt text - without a plugin
本文介绍如何使用 WP CLI(WordPress 命令行工具)快速识别 WordPress 网站中缺少特色图片或 alt 文本的博客文章。作者演示了通过命令行执行自定义查询,直接获取这些不符合 SEO 最佳实践的帖子列表。该方法避免了安装额外插件的需求,适合开发者或运维人员批量检查和修复内容问题。使用 WP CLI 能显著提升内容审核效率,尤其适用于拥有大量文章的站点。
Terence Eden
WordPress 有“特色图片”的概念,这些图片在社交媒体上分享博客文章时会显示出来,在某些主题中还会作为“英雄图”出现。
如何快速轻松地找到没有特色图片的文章?
为此,我使用 WP CLI——它允许你通过命令行运行复杂的 WordPress 操作和查询。安装 WP CLI 后就可以开始使用了。
缺失的图片
在命令行中运行:
wp eval 'foreach(get_posts(array("post_type"=>"post","post_status"=>array("publish"),"posts_per_page"=>-1,)) as $post){if(get_the_post_thumbnail($post)==""){$post_type_object=get_post_type_object($post->post_type);$link=admin_url(sprintf($post_type_object->_edit_link . "&action=edit", $post->ID));echo $post->post_date . " " . $link . " " . $post->post_title . "\n";}}'以下是代码,格式稍微更易读一些:
foreach (
get_posts(
array( "post_type" => "post",
"post_status" => array("publish"),
"posts_per_page" => -1,
)
) as $post) {
if( get_the_post_thumbnail( $post)== "" ) {
$post_type_object = get_post_type_object( $post->post_type );
$link = admin_url( sprintf( $post_type_object->_edit_link . "&action=edit", $post->ID ) ) ;
echo $post->post_date . " " . $link . " " . $post->post_title . "\n";
}
}这将输出:
2024-05-02 12:34:11 https://example.com/wp-admin/post.php?post=123&action=edit "A post about sausages"
2023-09-13 20:55:52 https://example.com/wp-admin/post.php?post=456&action=edit "I like cheese"
2021-12-31 15:43:33 https://example.com/wp-admin/post.php?post=789&action=edit "Touching computers"然后你可以逐一编辑这些文章,添加特色图片。
缺失的替代文本(Alt Text)
添加替代文本意味着无法看到图片的人仍能理解图片所代表的内容。以下是一条命令,用于查找所有缺少替代文本的特色图片:
wp eval 'foreach (get_posts(array("post_type"=>"post","post_status"=>array("publish"),"posts_per_page" => -1,)) as $post){if(simplexml_load_string(get_the_post_thumbnail($post))["alt"]==""){$post_type_object=get_post_type_object($post->post_type);$link=admin_url(sprintf($post_type_object->_edit_link . "&action=edit",$post->ID));echo $post->post_date . " " . $link . " " . $post->post_title . "\n";}}'以更易读的形式如下:
foreach (
get_posts(
array( "post_type" => "post",
"post_status" => array("publish"),
"posts_per_page" => -1,
)
) as $post) {
if( simplexml_load_string( get_the_post_thumbnail( $post ) )["alt"] == "") {
$post_type_object = get_post_type_object( $post->post_type );
$link = admin_url( sprintf( $post_type_object->_edit_link . "&action=edit", $post->ID ) ) ;
echo $post->post_date . " " . $link . " " . $post->post_title . "\n";
}
}同样,它会列出文章的发布时间、编辑链接和标题。
现在,请原谅我,我有大约 873 篇文章需要更新 🤯
需要完整排版与评论请前往来源站点阅读。