【问题标题】:How to add rel atribute to the links in gutenberg gallery如何将 rel 属性添加到古腾堡画廊中的链接
【发布时间】:2018-12-13 13:34:37
【问题描述】:

如何在我的网站上制作 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】:
    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;
    }
    

    【讨论】:

    • 这个函数和fancybox 3 的data-fancybox 属性配合得很好。你可以修改“title 属性”。
    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    猜你喜欢
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 2019-02-16
    • 2021-08-24
    • 2020-03-13
    • 1970-01-01
    • 2023-02-11
    • 2019-08-19
    相关资源
    最近更新 更多