我有一个自定义过滤器,它将图像#ID附加到图像href的末尾,但它返回2个url。
有人能看出我做错了什么吗?
function modify_attachment_link($markup, $id, $size, $permalink, $icon, $text) {
    $pos = strpos($markup, "href") + 6;
    $remainstring = substr($markup, $pos);
    $pos2 = strpos($remainstring, "\'");
    $url = substr($remainstring, 0, $pos2);
    $url .= "#image-".$id;
    return substr_replace($markup, \'\' . $url . \'\', $pos, strlen(\'\'));
}
add_filter( \'wp_get_attachment_link\', \'modify_attachment_link\', 10, 6 );
 问候:)
 
                SO网友:Charles Clarkson
                strlen(\'\') 在里面substr_replace 将始终评估为0, 这不能替代任何东西。它只是在$pos.
您可以使用此选项:
function modify_attachment_link( $markup, $id ) {
    $pos = strpos( $markup, \'href\' ) + 6;
    $remainstring = substr( $markup, $pos );
    $pos2 = strpos( $remainstring, "\'" );
    $url = substr( $remainstring, 0, $pos2 );
    $url_length = strlen( $url );
    $url .= "#image-$id";
    return substr_replace( $markup, $url, $pos, $url_length );
}