最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

attachments - Change attached img anchor link to post link

programmeradmin4浏览0评论

When I attached images, it nested in this hierarchy of html tags :

<div id="attachment_ID" class="alignnone">
    <a class="image-ID" href="">
        <img class="yada yada" src="" alt="" srcset="more-image-links.jpg" sizes="yada ya da">
    </a>
</div>

So I want to manipulate the anchor into <a href="post-link"

I know it is required to filter something like :

function replace_a_link( $url ) {
    // Some codes here.
    return $url;
}
add_filter('the_content', 'replace_a_link');

But, I don't know the exact story :/

When I attached images, it nested in this hierarchy of html tags :

<div id="attachment_ID" class="alignnone">
    <a class="image-ID" href="https://image-link.jpg">
        <img class="yada yada" src="https://image-link.jpg" alt="" srcset="more-image-links.jpg" sizes="yada ya da">
    </a>
</div>

So I want to manipulate the anchor into <a href="post-link"

I know it is required to filter something like :

function replace_a_link( $url ) {
    // Some codes here.
    return $url;
}
add_filter('the_content', 'replace_a_link');

But, I don't know the exact story :/

Share Improve this question edited Apr 10, 2020 at 5:35 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Apr 10, 2020 at 5:30 Hsu Pyae NaungHsu Pyae Naung 1
Add a comment  | 

1 Answer 1

Reset to default 0

You may try the filter wp_get_attachment_link

// definition: apply_filters( 'wp_get_attachment_link', "<a href='" . esc_url( $url ) . "'>$link_text</a>", $id, $size, $permalink, $icon, $text, $attr );

// this filter provide a max. of 7 arguments, I use 2 in the example
add_filter( 'wp_get_attachment_link', 'q363693_update_attachment_link', 10, 2 );
function q363693_update_attachment_link( $url, $id ) {
   // some codes
   $new_link = "<a href='" . get_permalink( $id ) . "'>your own text or anything you need or add more arguments to use the original text...</a>";

   return $new_link;
}
发布评论

评论列表(0)

  1. 暂无评论