我如何创建一个文本链接到wordpress帖子中的第一个图像附件页面,而不试图弄清楚slug发布后的内容。我意识到我可以将图像链接到他们的附件页面,但我不能创建文本链接。这可能吗?
答案 0 :(得分:3)
想出来:
<?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;
}
}
}
?>