2012-05-22 131 views
0

我需要过滤器的自定义类到wp_get_attachment_link。所以我这样:WordPress添加过滤器到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); 
} 

所以,我的意思是,如果($永久)我并不需要添加自定义类实现此功能。

回答

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

这可能工作