【问题标题】:Change html structure of all img tags in Wordpress更改 Wordpress 中所有 img 标签的 html 结构
【发布时间】:2012-12-27 17:56:30
【问题描述】:

我有一个 Wordpress 博客,正在尝试实现 foresight.js 图像脚本。简而言之,我需要定位所有帖子图像,将src, width, & height 属性替换为data-src, data-width, & data-height 属性。然后我需要复制图像行并将其包装在<noscript> 标签中。这是我试图让 Wordpress 生成/创建的结构:

<img data-src="wordpress/image/url/pic.jpg" data-width="{get width of image with PHP & pass-in that value here} data-height="{get height of image with PHP and pass-in that value here}" class="fs-img">
<noscript>
    <img src="wordpress/image/url/pic.jpg">
</noscript>

More info can be found at foresight.js' website.

我已经搜索了 Wordpress 代码,我能找到的最佳途径是使用过滤器(即 'get_image_tag' 和 'image_tag')来修改 html Wordpress 为每个图像输出。我在想这些选项之一应该可以工作,或者我可以使用正则表达式进行一些模式匹配(我知道,不理想),输入preg_replace,然后将其注入@ 987654329@过滤器。

我已经尝试了其中的一些选项,但都无法正常工作。有人可以提供一些帮助吗?

'get_image_tag' 尝试:

在网上找到了这个特定的,但需要对其进行修改以适应我的逻辑(参见上面的结构)...无法理解 preg_replace 数组自己在做什么。

<?php function image_tag($html, $id, $alt, $title) {
    return preg_replace(array(
        '/'.str_replace('//','\/\/',get_bloginfo('url')).'/i',
        '/\s+width="\d+"/i',
        '/\s+height="\d+"/i',
        '/alt=""/i'
    ),
    array(
        '',
        '',
        '',
        alt='"' . $title . '"'
    ),
    $html);
}
add_filter('get_image_tag', 'image_tag', 0, 4);
?>

另一个“get_image_tag”尝试:

<?php function get_image_tag($id, $alt, $title, $align, $size='full') {
    list($width, $height, $type, $attr) = getimagesize($img_src);
    $hwstring = image_hwstring($width, $height);

    $class = 'align' . esc_attr($align) . ' size-' . esc_attr($size) . ' wp-image-' . $id;
    $class = apply_filters('get_image_tag_class', $class, $id, $align, $size);

    $html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" title="' . esc_attr($title).'" data-width="' . $width . '" data-height="' . $height . '" class="' . $class . ' fs-img" />';
    $html = apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size);

    return $html;
}
?>

模式匹配尝试:

尝试在这个上创建我自己的正则表达式,但不确定它是否正确。

<?php function restructure_imgs($content) {
    global $post;
    $pattern = "/<img(.*?)src=('|\")(.*?).(bmp|gif|jpeg|jpg|png)(|\")(.*?)>/i";

    list($width, $height, $type, $attr) = getimagesize($2$3.$4$5);
    $hwstring = image_hwstring($width, $height);

    $replacement = '<img$1data-src=$2$3.$4$5 title="'.$post->post_title.'" data-width="'.$width.'" data-height="'.$height.'" class="fs-img"$6>';
    $content = preg_replace($pattern, $replacement, $content);
    return $content;
}
add_filter('the_content', 'restructure_imgs');
?>

很遗憾,这些示例中的任何一个都无法正常工作。任何帮助或分享您预先编写的脚本/功能将不胜感激!感谢您帮助学生学习!

【问题讨论】:

  • 您不应该使用正则表达式解析 HTML,请参阅 here

标签: php regex wordpress function preg-replace


【解决方案1】:

您的方法是在页面渲染之前,我认为在wordpress 中并非不可能,但是您可以在页面使用javascript 渲染之后 执行相同的操作。 我尝试使用 jQuery 来做到这一点,并且 ti 似乎可以工作:

<!-- Example image -->
<img src="http://fotografia.deagostinipassion.com/resources/photos/2/2n/ilCielo.JPG" width="200px" height="300px">

<!-- Import jquery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>


<script>

$(document).ready(function () {

    $("img").wrap(function() {

        $(this).wrap(function() {

            var newimg = '<img data-src="' + $(this).attr('src') + '" data-width="' + $(this).attr('width') + '" data-height="' + $(this).attr('height') + '" class="fs-img">';
            return newimg;  

        });

        return '<noscript>';
    });

});

</script>

如果你需要更多帮助,可以问我。

【讨论】:

    猜你喜欢
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2017-07-21
    相关资源
    最近更新 更多