我正在尝试创建一个函数,将其放置在模板页面中,以输出帖子的附加图像。我想让图像显示为主题指定的缩略图大小,并链接到全尺寸图像,以便我可以给它们一个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 关于图像标记。
最合适的回答,由SO网友:5t3ph 整理而成
感谢@Rarst带领我正确地回答了这个问题!
如果var\\u dump()返回不起作用的内容,尤其是wp\\u get\\u attachment\\u img\\u src(),会得到什么?是否启用了WP\\U调试?-稀有的
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 needed to be just = not .= to start out the variable
$content = \'<div class="productimgs">\';
foreach ( $attachments as $attachment ) {
// The correct function to retrieve the needed URLs
$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>\';
}
// Most importantly, this needed to be an echo not a return
echo $content;
}
再次感谢Rarst引导我找到答案!