サポート » テーマ » 投稿画面で追加した画像を出力したい。

6件の返信を表示中 - 1 - 6件目 (全6件中)
  • トピック投稿者 acrnym

    (@acrnym)

    nobitoさん

    返信いただき誠にありがとうございます。
    この方法を試してみたのですが、よくわからない画像が表示されてしまいます。

    どのように設定すればいいでしょうか?

    この方法を試してみたのですが、よくわからない画像が表示されてしまいます。

    テーマでどのようにコードを記述して、結果がどうだったのかを具体的に書き込んでもらえますか?

    トピック投稿者 acrnym

    (@acrnym)

    nobitoさん申し訳ありません。下記に記しました。

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div id="container">
    <section>
    <?php $images =& get_children( 'post_type=attachment&post_mime_type=image' );
    
    $videos =& get_children( 'post_type=attachment&post_mime_type=video/mp4' );
    
    if ( empty($images) ) {
    	// 添付ファイルが無かった
    } else {
    	foreach ( $images as $attachment_id => $attachment ) {
    		echo wp_get_attachment_image( $attachment_id, 'full' );
    	}
    }
    
    //  添付ファイルが無い場合のコードを書かない方法:
    
    foreach ( (array) $videos as $attachment_id => $attachment ) {
    	echo wp_get_attachment_link( $attachment_id );
    }?>
    </section>
      <div class="another">
      <?php query_posts('showposts=100&cat=6'); while(have_posts()) : the_post(); ?>
        <a href="<?php the_permalink(); ?>">
          <?php if (has_post_thumbnail()): ?>
            <?php the_post_thumbnail('thumbnail'); ?>
          <?php endif ?>
          </a>
      <?php endwhile; ?>
      </div><!--another-->
    </div><!--container-->

    このようなコードを書きました。<div class=”another”></div>の部分はその他の記事を表示したものです。
    結果は、その投稿にアップした画像ではなく、wordpressのメディアにアップロードされている画像が3枚出力されました。

    <div class="another"></div>は、wp_reset_query()を追加したほうがいいです。

    <?
    php endwhile;
    wp_reset_query();
    ?>

    画像は、

    $images = get_children( 'post_type=attachment&post_mime_type=image&post_parent='. get_the_ID() );
    if( ! empty( $images ) ) {
    	foreach ( $images as $attachment_id => $attachment ) {
    		echo wp_get_attachment_image( $attachment_id, 'full' );
    	}
    }

    のように、post_parentを指定するといいと思います。

    トピック投稿者 acrnym

    (@acrnym)

    無事出来ました!ありがとうございます!

6件の返信を表示中 - 1 - 6件目 (全6件中)
  • トピック「投稿画面で追加した画像を出力したい。」には新たに返信することはできません。