サポート » 使い方全般 » “get_the_post_image”で取得した画像にリンクやテキストを追加表示させたい

  • 解決済 silver_kir

    (@silver_kir)


    以下の手順で、エントリーの特定部位にギャラリーを表示しているのですが、以下の仕様を追加したいと考えています。
    その実現に際して皆様のお知恵をお貸しいただければと思い投稿させていただきました。

    追加希望
    ・表示される縮小画像(現状だと’thumbnail’指定)の画像をクリックすることで拡大画像が開けるよう自動的にリンクさせたい。
    ・画像の下に説明文を表示したい。 ALTタグの内容でいいので写真の下にテキストを自動で表示させることはできないでしょうか?

    index.php側ソース

    <ul class="contents_entry">
    <?php get_the_post_image(get_the_ID(), 'thumbnail', 0, 6); ?>
    </ul>

    functions.php側追加ソース

    /**メディア内の写真を掲載 */
    function get_the_post_image($postid,$size='full',$offset=0,$numberimages=-1) {
    	$args = array(
    		'post_type'			=> 'attachment',
    		'post_mime_type'	=> 'image',
    		'orderby'			=> 'menu_order',
    		'order'				=> 'ASC',
    		'post_status'		=> null,
    		'numberposts'		=> $numberimages,
    		'offset'			=> $offset,
    		'post_parent'		=> $postid,
    	);
    
    	$attachments = get_posts( $args );
    	if ( $attachments ) {
    		foreach ( $attachments as $attachment ) {
    			echo '<li>';
    			echo wp_get_attachment_image( $attachment->ID, $size );
    			echo '</li>';
    		}
    	}
    }

    宜しくお願い致します。

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • 添付ファイルの画像のALT(代替テキスト)はget_post_meta($post_id, '_wp_attachment_image_alt', true);で取得できます。
    タイトル・キャプション・説明を取得したい場合は、添付ファイルのpost_title, post_excerpt, post_content をそれぞれ取得すれば良いと思います。

    /**
    * メディア内の写真を掲載するHTMLを出力
    * @param int $postid 親投稿のID
    * @param string $size 表示する画像のサイズ
    * @param string $link_size リンク先の画像のサイズ
    * @param int $offset 何件目から取得するか
    * @param int $numberimages 何件取得するか
    * @return void
    */
    function get_the_post_image($postid,$size='full',$link_size='full',$offset=0,$numberimages=-1) {
        $args = array(
            'post_type'         => 'attachment',
            'post_mime_type'    => 'image',
            'orderby'           => 'menu_order',
            'order'             => 'ASC',
            'post_status'       => null,
            'numberposts'       => $numberimages,
            'offset'            => $offset,
            'post_parent'       => $postid,
        );
    
        $attachments = get_posts( $args );
        if ( $attachments ) {
            foreach ( $attachments as $attachment ) {
                // $link_image_src にはリンク先画像のパスが入る
                $link_image_src = wp_get_attachment_image_src($attachment->ID, $link_size);
                // $thumb_image_src には 表示画像のパスが入る
                $thumb_image_src = wp_get_attachment_image_src($attachment->ID, $size);
    
                // 代替テキストを取得
                $alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
                // タイトルを取得
                $title = $attachment->post_title;
    
                // $captionには表示したいテキストが代入されます。
                // $caption に画像の代替テキストを入れます。
                $caption = $alt;
                // $caption に画像の説明を入れたいときはこれ。
                // $caption = $attachment->post_content;
                // $caption に画像のタイトルを入れたいときはこれ。
                // $caption = $title;
                // $caption にキャプションを入れたいときはこれ。
                // $caption = $attachment->post_except;
                echo '<li>';
                echo '<a href="' . $link_image_src[0] . '" target="_blank">';
                echo '<img src="' . $thumb_image_src[0] . '" alt="' . $alt . '" title="' . $title . '" width="' . $thumb_image_src[1] . '" height="' . $thumb_image_src[1] . '" />';
                echo '</a><br />';
                echo $caption;
                echo '</li>';
            }
        }
    }
    トピック投稿者 silver_kir

    (@silver_kir)

    ご提示いただきましたfunctions.phpの内容を使って目的の仕様が実現できました。
    また、captionに関しては多数の利用例も組み込んでいただき非常に判りやすく整理していただけていて助かります。

    本当にありがとうございました!

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • トピック「“get_the_post_image”で取得した画像にリンクやテキストを追加表示させたい」には新たに返信することはできません。