将 ALT 属性替换为图像标题 + ACF 关系 URL(如果存在)

Replace ALT attribute with image caption + ACF relationship URL if it exists

我可以使用此 WordPress 功能,但它在测试中有 1 个明显问题,我敢肯定还有许多其他格式问题。我已将此功能从各个部分中提取出来,但非常感谢有关清理它的反馈。

函数的用途

  1. 修改 WordPress 图库中任何图像的 ALT 属性
  2. 获取图像标题字段并添加到 ALT
  3. 获取 ACF 关系字段“摄影师”,如果它有值 (URL)
  4. 在 ALT 末尾添加“查看摄影师”HTML,前提是该图像已指定给摄影师
  5. Returns 如果图像没有分配标题或 ACF 关系,则不会出现错误

为什么我要用 HTML 修改 alt 标签?我们目前使用 Facncybox 2.1.5,它从 alt 标签中提取字幕。

输出错误我需要帮助

该函数获取标题和摄影师 url 并将它们添加到图库中的每张图片。然而,如果图像 A 有标题数据和摄影师数据,但图像 B 有标题但没有分配摄影师,图像 B 仍然有“查看摄影师”link 显示在 alt 字段中,URL 是图片A.

如何确保函数检查每张图像,然后在移动到下一张图像之前完全结束检查,这样它就不会将摄影师关系 URL 从上一张图像拉到下一张图像如果下一张图片没有指定 ACF 关系?

function wpdocs_replaceALT( $content ) {
    if ( is_single() && in_the_loop() && is_main_query() ) {
 
        libxml_use_internal_errors( true );
 
        $post = new DOMDocument();
        $post->loadHTML( $content );
 
        $images = $post->getElementsByTagName( 'img' );
 
        foreach ( $images as $image ) {
            
            $src = $image->getAttribute( 'src' );
                $image_id = attachment_url_to_postid( $src );
                $media = get_post( $image_id );
                $caption = $media->post_excerpt;
                $photographer_relationship = get_field('photographer', $image_id);
                $photographer = (is_array($photographer_relationship) && !empty($photographer_relationship)) ? array_shift($photographer_relationship) : null;
                if ( !empty($photographer)) {
                    $link_to_photographer = get_permalink($photographer);
                    }
                if ($media && !empty($media->post_excerpt)) {
                    $view_photog = '<a class="learn-more-photog-link" target="_blank" href="'.$link_to_photographer.'">View Photographer</a>';
                }
                $link_text = ''.$caption.''.$view_photog.'';
            
            
            if ( empty( $image->getAttribute( 'title' ) ) ) {
 
                
                $image->setAttribute( 'title', $link_text);
            }
        }
        $content = $post->saveHTML();
        return $content;
    }
}
add_filter( 'the_content', 'wpdocs_replaceALT' );

以下解决方案 基于 Nathan Dawson 的反馈。

function wpdocs_replaceALT( $content ) {
  if ( is_single() && in_the_loop() && is_main_query() ) {

    libxml_use_internal_errors( true );

    $post = new DOMDocument();
    $post->loadHTML( $content );
    $images = $post->getElementsByTagName( 'img' );

    foreach ( $images as $image ) {
        
      $view_photog = '';
      $src = $image->getAttribute( 'src' );
      $image_id = attachment_url_to_postid( $src );
      $media = get_post( $image_id );
      $caption = $media->post_excerpt;
      $photographer_relationship = get_field( 'photographer', $image_id );
      $photographer = ( is_array( $photographer_relationship ) && !empty( $photographer_relationship ) ) ? array_shift( $photographer_relationship ) : null;
        
      if ( !empty( $photographer ) ) {
        $link_to_photographer = get_permalink( $photographer );
        $view_photog = '<a class="learn-more-photog-link" target="_blank" href="' . $link_to_photographer . '">View Photographer</a>';
      }

      $link_text = '' . $caption . '' . $view_photog . '';

      if ( empty( $image->getAttribute( 'title' ) ) ) {

      $image->setAttribute( 'title', $link_text );
      }
    }
    $content = $post->saveHTML();
    return $content;
  }
}
add_filter( 'the_content', 'wpdocs_replaceALT' );

当循环遍历图像时,$view_photog 变量仅在满足特定条件时设置。您需要在循环的每次迭代中重置变量,否则当条件不满足时,将保留上次的值。

示例:

foreach ( $images as $image ) {
    $view_photog = '';