You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress如何获取文章当前实际使用的图片附件?

更高效优雅的WordPress当前文章使用图片获取方案

首先,你当前解析<img>标签的思路是可行的,但确实有更贴合WordPress生态的方法,不用自己手动处理DOM解析的细节,稳定性和可维护性更好:

方案1:利用WordPress内置函数提取URL并匹配附件ID

WordPress从4.4版本开始提供了wp_extract_urls()函数,可以直接从文章内容中提取所有URL,我们可以在此基础上筛选出属于当前站点的附件URL,再通过attachment_url_to_postid()转换成附件ID,最终拿到对应的附件对象:

function get_current_post_used_images( $post_id = null ) {
    if ( !$post_id ) {
        $post_id = get_the_ID();
    }
    $post_content = get_post_field( 'post_content', $post_id );
    $all_urls = wp_extract_urls( $post_content );
    
    $used_attachment_ids = array();
    foreach ( $all_urls as $url ) {
        // 只处理当前站点的附件URL
        if ( str_starts_with( $url, site_url() ) ) {
            $attachment_id = attachment_url_to_postid( $url );
            if ( $attachment_id && $attachment_id != $post_id ) { // 排除文章自身URL冲突的情况
                $used_attachment_ids[] = $attachment_id;
            }
        }
    }
    
    // 去重并按内容中出现顺序返回附件对象
    $used_attachment_ids = array_unique( $used_attachment_ids );
    return get_posts( array(
        'post__in' => $used_attachment_ids,
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'posts_per_page' => -1,
        'orderby' => 'post__in'
    ) );
}

这个方法的核心优势:

  • 不用自己写正则解析<img>标签,避免了不同编辑器(古腾堡、经典编辑器)生成的HTML结构差异问题
  • 依赖WordPress原生函数处理URL与附件的映射,兼容性和可靠性更强

方案2:针对古腾堡块编辑器的专属优化

如果你的站点使用古腾堡块编辑器,还可以直接从文章的块数据中提取图片块对应的附件ID,完全跳过HTML解析步骤,准确性更高:

function get_gutenberg_post_used_images( $post_id = null ) {
    if ( !$post_id ) {
        $post_id = get_the_ID();
    }
    $blocks = parse_blocks( get_post_field( 'post_content', $post_id ) );
    
    $used_attachment_ids = array();
    foreach ( $blocks as $block ) {
        // 处理独立图片块
        if ( $block['blockName'] === 'core/image' && isset( $block['attrs']['id'] ) ) {
            $used_attachment_ids[] = $block['attrs']['id'];
        }
        // 递归处理嵌套块(比如媒体&文本块、列块内的图片)
        elseif ( isset( $block['innerBlocks'] ) && !empty( $block['innerBlocks'] ) ) {
            $nested_ids = get_gutenberg_post_images_from_inner_blocks( $block['innerBlocks'] );
            $used_attachment_ids = array_merge( $used_attachment_ids, $nested_ids );
        }
    }
    
    $used_attachment_ids = array_unique( $used_attachment_ids );
    return get_posts( array(
        'post__in' => $used_attachment_ids,
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'posts_per_page' => -1,
        'orderby' => 'post__in'
    ) );
}

// 辅助递归函数处理嵌套块
function get_gutenberg_post_images_from_inner_blocks( $blocks ) {
    $ids = array();
    foreach ( $blocks as $block ) {
        if ( $block['blockName'] === 'core/image' && isset( $block['attrs']['id'] ) ) {
            $ids[] = $block['attrs']['id'];
        }
        if ( isset( $block['innerBlocks'] ) && !empty( $block['innerBlocks'] ) ) {
            $ids = array_merge( $ids, get_gutenberg_post_images_from_inner_blocks( $block['innerBlocks'] ) );
        }
    }
    return $ids;
}

这个方法的优势是完全基于块数据,不会漏掉古腾堡插入的图片,也不会误判外链图片,适合纯块编辑器的站点。

为什么不推荐手动解析<img>标签?

手动解析需要处理各种边缘情况:

  • 不同编辑器生成的src/srcset/data-src等多格式属性
  • 外链图片与站内附件的区分
  • 短代码包裹的图片(比如[caption]标签)
    而上面的方案已经帮你覆盖了这些场景,代码更健壮。

内容的提问来源于stack exchange,提问作者user64675

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:39:22