我正在尝试创建一个函数,将其放置在模板页面中,以输出帖子的附加图像。我想让图像显示为主题指定的缩略图大小,并链接到全尺寸图像,以便我可以给它们一个lightbox触发类(在本例中为thickbox)。
我目前只能使用the_attachment_link
它链接并显示图像,但不允许我像添加类一样自定义它。
以下是我当前的代码,包括什么有效,我认为应该有效,以及我尝试过的另一个解决方案:
function product_images() {
global $post;
$attachments = get_posts( array(
\'post_type\' => \'attachment\',
\'posts_per_page\' => 0,
\'post_parent\' => $post->ID,
\'exclude\' => get_post_thumbnail_id()
) );
if ( $attachments ) {
$content .= \'<div class="productimgs">\';
foreach ( $attachments as $attachment ) {
// What works to display image but doesn\'t allow customizing output
$imgthumb = the_attachment_link($attachment->ID, false);
$content .= $imgthumb;
/*What I WANT to work!
$imgthumb = wp_get_attachment_url($attachment->ID);
$content .= \'<a class="thickbox" href="\'.$imgthumb.\'"><img class="productimg" src="\'.$imgthumb.\'" /></a>\';
*/
/* Also tried
$imgthumb = wp_get_attachment_image_src( $attachment->ID, \'thumbnail\' );
$imgfull = wp_get_attachment_image_src( $attachment->ID, \'full\' );
$content .= \'<a class="thickbox" href="\'.$imgfull[0].\'"><img class="productimg" src="\'.$imgthumb[0].\'" /></a>\';
*/
}
$content .= \'</div>\';
}
return $content;
}提前感谢您的帮助!我已经试了好几个小时了。
我看过这个网站上的其他几篇文章,但没有一篇是同样的问题。我还浇了Digging into Wordpress article 关于图像标记。