preg_replace为wordpress图像链接添加基础显示

时间:2015-01-12 04:40:00

标签: php wordpress preg-replace

想知道是否有人可以在Wordpress中使用preg_replace来改变链接到图像功能的方式。

所以目前启用了mootools lazyload,我已经吐出来了:

<a href='http://localhost:8080/wp-content/uploads/2015/01/2014-11-26-20.52.53.jpg'><img width="150" height="150" src="http://localhost:8080/wp-content/uploads/2015/01/2014-11-26-20.52.53-150x150.jpg" class="attachment-thumbnail" alt="2014-11-26 20.52.53" /></a>

理想情况下,我希望所有缩略图链接都加载在Foundation Reveal Modal中,代码如下:

<a href="#" data-reveal-id="2014-11-26-20.52.53"> 
<img src="http://localhost:8080/wp-content/uploads/2015/01/2014-11-26-20.52.53-150x150.jpg">
</a>

<div id="2014-11-26-20.52.53" class="reveal-modal" data-reveal>
<img src="http://localhost:8080/wp-content/uploads/2015/01/2014-11-26-20.52.53.jpg">
<a class="close-reveal-modal">&#215;</a>
</div>

有什么建议吗?我尝试过屠宰一些类似的代码 - 但preg_replace并不是最容易学习的语法。

2 个答案:

答案 0 :(得分:0)

好的,所以我很接近:

add_filter('the_content', 'addreveal', 12);
add_filter('get_comment_text', 'addreveal');

function addreveal ($content){   
    global $post;
    $pattern = "/<a(.*?)href=('|\")([^>]*).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>(.*?)<\/a>/i";
    $replacement = '<a$1href="#" data-reveal-id="myModal">$7</a> <div id="myModal"       class="reveal-modal" data-reveal> <img src="$3.$4"> <a class="close-reveal-modal">&#215;</a>   </div> ';
    $content = preg_replace($pattern, $replacement, $content);
    return $content;

}

但是我对$ 7变量有点困惑 - 我想把它拆分为/并使文件名没有扩展名data-reveal-id。有什么建议吗?

答案 1 :(得分:0)

在为基金会6进行一些小调整之后,这对我有用。

add_filter( 'the_content', 'addreveal', 12 );
add_filter( 'get_comment_text', 'addreveal' );

function addreveal ($content) {
    global $post;
    $pattern = "/<a(.*?)href=('|\")([^>]*).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>(.*?)<\/a>/i";
    $replacement = '<a$1href="#" data-open="full">$7</a>
    <div id="full" class="full-image reveal" data-reveal>
        <div class="modal-container">
            <img src="$3.$4">
            <button class="close-button" data-close aria-label="Close reveal" type="button"></button>
        </div>
    </div>';
    $content = preg_replace($pattern, $replacement, $content);
    return $content;
}