我想自定义Wordpress默认图库,以便为每个项目设置如下格式:
<dl class="gallery-item imgteaser">
<a rel="lightbox-cats" href="http://mydomain.com/link/to/image.jpg" title="Image caption (if any)" name="image.jpg (image file name)">
<img width="150" height="150" src="http://mydomain.com/link/to/image-150x150.jpg" class="attachment-thumbnail" alt="002" title="002">
</a>
</dl>
基本上我想要链接(<a>
) 拥有:rel
用于激活lightbox脚本(由用户在每个库中手动定义为“lightbox猫”)href
链接到图像的直接URL(例如:http://mydomain.com/link/to/image.jpg)title
这是Wordpress图像标题name
这是图像文件名(例如:image.jpg)
例如,我不需要<a>
链接到附件页(类似于http://mydomain.com/?attachment=20)。我只需要它直接链接到图像URL。
使用筛选器并将其添加到functions.php
, 以下是我到目前为止所做的(非常混乱):http://pastebin。com/Q51W1BVr
我把它放进了pastebin,因为它有点太长(很抱歉,我不能把它作为链接,因为我在这里没有足够的信誉点:/)
虽然我相信修改输出的部分仅来自这里:
$i = 0; // Modified output from here
foreach ( $attachments as $id => $attachment ) {
$link = wp_get_attachment_link($id, $size, true, false);
if( ! empty($rel) ) { !!! // Add rel injection
$link = str_replace(\'<a href=\', "<a rel=\'$rel\' href=" .wp_get_attachment_url( $attachment->ID ). " alt=", $link);
} else {
$link = str_replace(\'<a href=\', "<a rel=\'$rel\' href=", $link);
}
$link = str_replace("title=\'", "title=\'" . wptexturize($attachment->post_excerpt) . "\' name=\'", $link);
$output .= "<{$itemtag} class=\'gallery-item imgteaser\'>
";
$output .= $link;
$output .= "</{$itemtag}>
";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= \'<br style="clear: both" />\';
}
$output .= "
<br style=\'clear: both;\' />
</div>\\n";
return $output;
}
以下是输出结果的混乱程度:<dl class="gallery-item imgteaser">
<a rel="lightbox-cats" href="http://localhost/test/wp-content/uploads/2012/09/002.jpg" alt="http://localhost/test/?attachment_id=36" title="" name="002">
<img width="150" height="150" src="http://localhost/test/wp-content/uploads/2012/09/002-150x150.jpg" class="attachment-thumbnail" alt="002" title="002"
</a>
</dl>
这是可行的,但它有不必要的alt="http://localhost/test/?attachment_id=36"
因为我不知道如何移除它。我觉得我的代码真的不够有效/不干净/