0

WordPressの投稿に添付ファイルとして画像を追加する必要があります。現在、画像は投稿コンテンツの一部として設定されていますが、ユーザーが添付リンクを介してその画像をダウンロードできるように、投稿に添付リンクを表示する必要があります。

ありがとうございました、

4

1 に答える 1

0

常に1つの画像しかない場合は、「注目の画像」として設定してから、次のように設定することをお勧めします。

$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'your-image-size' )

それから

echo $image[0] ;is your link 

完全なコード:

<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?>
<a href='<?php echo $image[0]; ?>'> my image link </a>

注目の画像を使用したくない場合-次の関数は、投稿の最初の画像を常に取得します

// Get URL of first image in a post

function postimage( $echo = true ) {
    $image = get_children( array(
        'post_parent' => get_the_ID(),
        'post_type' => 'attachment',
        'numberposts' => 1,
        'order' => 'asc',
        'orderby' => 'ID',
        'post_mime_type' => 'image',
    ) );
    $image_url = ( $image ) ? wp_get_attachment_url( current($image)->ID ) : "No Image";
    if( $echo )
        echo $image_url;
    else
        return $image_url;
}

複数の画像がある場合は、少し複雑になります。すべての添付ファイルを検索して、配列をループする必要があります。('numberposts' => -1を変更して、すべてを取得します)

于 2012-03-30T11:21:50.663 に答える