WordPress将过滤器添加到wp_get_attachment_link

时间:2012-05-22 21:19:16

标签: wordpress wordpress-theming

我需要自定义类来过滤到wp_get_attachment_link。所以我是这样的:

function modify_attachment_link( $markup ) {
global $post;
return str_replace( '<a href', '<a class="view" rel="galleryid-'. $post->ID .'" href', $markup );
}
add_filter( 'wp_get_attachment_link', 'modify_attachment_link' );

工作正常。但是,如果链接缩略图,我需要做的事情:附件页面 我的意思是在这种情况下我不需要自定义类。有什么帮助吗?

wp_get_attachment_link的核心功能是:

function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false ) {
$id = intval( $id );
$_post = & get_post( $id );

if ( empty( $_post ) || ( 'attachment' != $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) )
    return __( 'Missing Attachment' );

if ( $permalink )
    $url = get_attachment_link( $_post->ID );

$post_title = esc_attr( $_post->post_title );

if ( $text )
    $link_text = esc_attr( $text );
elseif ( $size && 'none' != $size )
    $link_text = wp_get_attachment_image( $id, $size, $icon );
else
    $link_text = '';

if ( trim( $link_text ) == '' )
    $link_text = $_post->post_title;

return apply_filters( 'wp_get_attachment_link', "<a href='$url' title='$post_title'>$link_text</a>", $id, $size, $permalink, $icon, $text );
}

所以我的意思是if($ permalink)我不需要为这个函数添加自定义类。

1 个答案:

答案 0 :(得分:2)

尝试

function modify_attachment_link( $markup, $id, $size, $permalink ) {
    global $post;
    if ( ! $permalink ) {
        $markup = str_replace( '<a href', '<a class="view" rel="galleryid-'. $post->ID .'" href', $markup );
    }
    return $markup;
}
add_filter( 'wp_get_attachment_link', 'modify_attachment_link', 10, 4 );

这可能有用