带链接的Wordpress图像附件

时间:2012-02-07 02:30:39

标签: image wordpress hyperlink attachment

尝试用附件/图片替换“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;
    }
}
}

?>

3 个答案:

答案 0 :(得分:2)

echo '<a href="'. get_attachment_link($attachment->ID) . '">'. wp_get_attachment_image($attachment->ID) .'</a>';

答案 1 :(得分:1)

<?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;
        }
        } 
        }
    ?>

感谢格伦提供找到答案的方法!

答案 2 :(得分:0)

wp_get_attachment_link( $attachment->ID );

这是一个简单,紧凑的WordPress API调用。它将wp_get_attachment_image()包装在锚标记中,链接到附件。