尝试用附件/图像替换单词"LINK TEXT HERE"。我该怎么做?
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'offset' => 0,
'orderby' => 'menu_order',
'order' => 'asc',
'post_status' => null,
'post_parent' => $post->ID,
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
if(wp_attachment_is_image( $attachment->ID )) {
echo '<a href="'. get_attachment_link($attachment->ID) . '">LINK TEXT HERE</a>';
break;
}
}
}
?>
echo '<a href="'. get_attachment_link($attachment->ID) . '">'. wp_get_attachment_image($attachment->ID) .'</a>';
<?php
$args = array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC',
'offset' => '1',
'numberposts' => 1
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
if(wp_attachment_is_image( $attachment->ID )) {
echo '<a href="'. wp_get_attachment_url($attachment->ID) . '" class="thumbnail">'. wp_get_attachment_image($attachment->ID) .'</a>';
break;
}
}
}
?>
感谢Glen提供了找到答案的方法
wp_get_attachment_link( $attachment->ID );
这一切在一个简单,紧凑的WordPress API调用。它将wp_get_attachment_image()
包装在锚定标记中,链接到附件。