开发者

remove anchor element around Wordpress images with filter (or jquery)

I have an anchor element like this:

<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>
开发者_如何学Go

(It's the standard way of Wordpress to embed uploaded pictures in a post.)

I want to remove the anchor around the image element, but keep the image. I simply want the image to show without being clickable.

This could be done either with a filter for the content of a post in Wordpress or after the page is loaded with javascript. Filtering in Wordpress would be preferred. I have no idea how to do either of those two options.


Find helpful code here:

Tried out, but caused invalid code.

Your code in ../your_theme/functions.php would look like this:

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');


Go into your WP theme's folder, edit "functions.php". Add code like this:

function remove_anchor($data)
{
    // the code for removing the anchor here

    // (not sure if you need help there, too).  

    // you will work on the $data string using DOM or regex
    // and then return it at the end


    return $data;
}

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

The add_filter means that every time a post is displayed, the remove_anchor function is called.

jQuery is probably easier, you just need to identify the images and not make them clickable (this is untested)

$(document).ready(function()
{
    $('#post a.some-class-name').click(function()
    {
        return false;
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜