php - 使用过滤器(或 jquery)删除 Wordpress 图像周围的 anchor 元素

标签 php wordpress filter

我有一个这样的 anchor 元素:

<a href="/link-to-image/" rel="attachment wp-att-7076"><img src="/uploads/img.jpg" alt="" title="" width="1268" height="377" class="alignnone size-full wp-image-7076" /></a>

(这是 Wordpress 在帖子中嵌入上传图片的标准方式。)

我想移除图像元素周围的 anchor ,但保留图像。我只是想让图像显示而无需点击。

这可以通过对 Wordpress 中的帖子内容进行过滤器或在使用 javascript 加载页面之后来完成。在 Wordpress 中过滤将是首选。我不知道如何执行这两个选项中的任何一个。

最佳答案

查找有用的代码 here :

已尝试,但导致无效代码。

../your_theme/functions.php 中的代码如下所示:

function remove_anchor($content) {
    // the code for removing the anchor here
    $content =
        preg_replace(
            array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}', '{</a>}'),
            array('<img',''),
            $content
        );
    return $content;
}

// then use WP's filter/hook system like this:
add_filter('the_content', 'remove_anchor');

关于php - 使用过滤器(或 jquery)删除 Wordpress 图像周围的 anchor 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3829786/

相关文章:

php - 更新特定行textarea和mysql

css - 覆盖容器

c# - BindingSource.Filter 最大长度

apache - 通过 apache 服务器内的服务器端过滤动态包含 piwik 代码

javascript - reactjs中的输入过滤

php - 如何检查许多查询结果是否成功

PHP 目标 iframe 并给它一个 ?cat

wordpress - 显示 Woocommerce 可变产品尺寸

php - 该代码可被利用吗? (PHP)

javascript - 滑回顶部