如何将rel属性添加到gutenberg库中的链接

问题描述 投票:0回答:1

如何在我的网站上制作工作fancybox 3。我将这段代码用于旧画廊:

add_filter('wp_get_attachment_link', 'rc_add_rel_attribute');
function rc_add_rel_attribute($link) {
global $post;
return str_replace('<a href', '<a data-fancybox="images" href', $link);
}

现在这段代码无效。

wordpress image-gallery fancybox-3 wordpress-gutenberg gutenberg-blocks
1个回答
1
投票
add_filter('the_content', 'aggiungi_fancybox');

function aggiungi_fancybox($content) {
       global $post;
       $pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
       $replacement = '<a$1href=$2$3.$4$5 data-fancybox="gallery" title="'.$post->post_title.'"$6>';
       $content = preg_replace($pattern, $replacement, $content);
       return $content;
}
© www.soinside.com 2019 - 2024. All rights reserved.