添加:
$first_attachment = reset($attachments);
$first_attachment_id = $first_attachment->ID;
$first_attachment_title = $first_attachment->post_title;
以下内容:
$nbImg = count($attachments);
现在,您可以通过以下方式返回链接:
echo wp_get_attachment_link( $first_attachment_id, \'\' , true, false, $first_attachment_title );
如果需要一种自定义链接标记,可以查看相关函数:
http://codex.wordpress.org/Function_Reference/wp_get_attachment_linkEdit:
没有必要获取两次附件,据我所知,您只需要知道您有多少个附件以及第一个附件的URL。因此,您可以替换:
<?php
$args = array(
\'post_type\' => \'attachment\',
\'numberposts\' => -1,
\'offset\' => 0,
\'orderby\' => \'menu_order\',
...
break;
}
}
}
?>
使用:
<?php
$attachments = get_children(array(\'post_parent\'=>$post->ID));
$nbImg = count($attachments);
$first_attachment = reset($attachments);
$first_attachment_id = $first_attachment->ID;
$first_attachment_title = $first_attachment->post_title;
?>
之后,您可以放置链接:
<?php echo wp_get_attachment_link( $first_attachment_id, \'\' , true, false, $first_attachment_title ); ?>