首页 > 解决方案 > 将 ALT 属性替换为图片标题 + ACF 关系 URL(如果存在)

问题描述

我有这个 WordPress 功能,但它在测试中有 1 个明显的问题,而且我确信还有许多其他格式问题。我已经将这个功能从各个部分组合在一起,但非常感谢有关清理它的反馈。

函数的目的

  1. 修改 WordPress 画廊中任何图像的 ALT 属性
  2. 获取图片标题字段并添加到 ALT
  3. 如果有值(URL),则获取 ACF 关系字段“photographer”
  4. 仅当该图像已分配给摄影师时,才将“查看摄影师”HTML 添加到 ALT 末尾
  5. 如果图像没有分配标题或 ACF 关系,则不返回错误

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

输出错误我需要帮助

该函数确实获取标题和摄影师 URL,并将它们添加到画廊中的每个图像中。但是,如果图像 A 有标题数据和摄影师数据,但图像 B 有标题但未分配摄影师,则图像 B 仍然在 alt 字段中显示“查看摄影师”链接,其 URL 是图像 A 的 URL。

如何确保该功能检查每张图像,然后在移动到下一张图像之前完全结束其检查,以便如果下一张图像没有,它不会将摄影师关系 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' );

标签: phpwordpressfunctiondomadvanced-custom-fields

解决方案


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

例子:

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

推荐阅读