代码之家  ›  专栏  ›  技术社区  ›  Madeirense

Wordpress帖子正文图像的巨大弹出窗口

  •  0
  • Madeirense  · 技术社区  · 9 年前

    我正在研究WordPress主题,我想知道是否有一种方法可以在Magnific Popup中打开由编辑器插入到帖子正文中的图像。因此,我通过TinyMCE编辑器在帖子中插入的任何图像都会在点击前端时在Magnific Popup上打开。

    1 回复  |  直到 9 年前
        1
  •  0
  •   Blaine    9 年前

    这个问题有几种不同的解决方法。我在下面概述了几个。

    选项1

    过滤内容并应用可通过Magnific Popup作为目标的HTML属性。

    我们可以 take a cue from this article 和杠杆 the_content

    “The_content”过滤器用于过滤帖子的内容 从数据库中检索后,打印到 屏幕。

    函数.php

    将以下内容添加到 functions.php .

    function prefix_content_gallery( $content ) {
        global $post;
    
        $pattern     = "/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
        $replacement = '<a$1href=$2$3.$4$5 rel="lightbox" title="'.$post->post_title.'"$6>';
        $content     = preg_replace( $pattern, $replacement, $content );
    
        return $content;
    }
    add_filter( 'the_content', 'prefix_content_gallery' );
    

    JS语言

    $('.entry-content').magnificPopup({
        type: 'image',
        delegate: '[rel="lightbox"]',
        gallery: {
            enabled: true
        }
    });
    

    View Example


    选项2

    另一种选择是有选择地将CSS类分配给应该是图像库一部分的链接。

    1. 添加要发布的媒体,并在“附件显示设置”下将“链接到”设置为“媒体文件”。

    enter image description here

    1. 将图像插入帖子后编辑该图像。

    enter image description here

    1. 向包装图像的链接添加CSS类。

    enter image description here

    JS语言

    $('.entry-content').magnificPopup({
        type: 'image',
        delegate: '.entry-gallery',
        gallery: {
            enabled: true
        }
    });
    

    View Example