php - 将 ALT 属性替换为图片标题 + ACF 关系 URL(如果存在)
问题描述
我有这个 WordPress 功能,但它在测试中有 1 个明显的问题,而且我确信还有许多其他格式问题。我已经将这个功能从各个部分组合在一起,但非常感谢有关清理它的反馈。
函数的目的
- 修改 WordPress 画廊中任何图像的 ALT 属性
- 获取图片标题字段并添加到 ALT
- 如果有值(URL),则获取 ACF 关系字段“photographer”
- 仅当该图像已分配给摄影师时,才将“查看摄影师”HTML 添加到 ALT 末尾
- 如果图像没有分配标题或 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' );
解决方案
循环图像时,$view_photog
仅在满足某些条件时才设置变量。您需要在循环的每次迭代中重置变量,否则当条件不满足时,该值将保留上次的值。
例子:
foreach ( $images as $image ) {
$view_photog = '';
推荐阅读
- paypal - 贝宝 PDT 问题
- vhdl - VHDL 函数在精细中的行为与在仿真中的行为不同
- python - 使用函数和 while 循环,我如何将许多票价插入列表形式。用户可以决定需要插入多少票价
- kubernetes - srimzi operator 0.20 kafka 'useServiceDnsDomain' 没有效果
- firebase - 如何根据查询参数路由到Vue路由器中的不同组件
- bit-manipulation - 基于解码 RLE(按位)函数对编码函数进行编程
- git - Jenkins 无法从远程存储库 BitBucket 读取
- python - discord.py 成员角色的 If Else 语句
- typescript - Typescript 条件分型奇怪的错误案例
- crystal-reports - 当内容从字段打印到屏幕时,垂直调整组标题以适应内容