サポート » 使い方全般 » 記事に埋め込んだギャラリーの中から1枚を抜き出してアーカイブに表示

  • 解決済 spaci654

    (@spaci654)


    早い話が、TwentyTenテーマで、’gallery’スラッグに入れられた投稿の中にギャラリーが埋め込まれていると、アーカイブ(loop.php)で、「この記事には**枚の画像があります」という表示とともに、ギャラリーの中から1枚だけ画像を抜粋表示するというデフォルト機能がありますよね。

    あれを、TwentyTenじゃなく、自作テーマでやりたいのですが、どのようなコードを書いたらよいのかわかりません。

    ぐぐったりして情報を探すと、アイキャッチ画像から引っ張ってくる方法や、投稿の本文内に使われている画像を引っ張ってくることは出来ましたが、ギャラリーから引っ張って来る方法がわからないのです。

    TwentyTenの子テーマを作れば解決するのかもしれませんが、あくまでどういうコードを書けばいいのか教えていただけたら、大変ありがたいです。

    2010のloop.phpのこの↓あたりにヒントがあると思うのですが。
    functions.phpのどの部分を参考にすればいいのか、という検討が上手く付けられなくて行き詰っている状態です。

    <?php /* How to display posts in the Gallery category. */ ?>
    
    	<?php if ( in_category( _x('gallery', 'gallery category slug', 'twentyten') ) ) : ?>
    		<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    			<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    
    			<div class="entry-meta">
    				<?php twentyten_posted_on(); ?>
    			</div><!-- .entry-meta -->
    
    			<div class="entry-content">
    <?php if ( post_password_required() ) : ?>
    				<?php the_content(); ?>
    <?php else : ?>
    				<?php
    					$images = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
    					if ( $images ) :
    						$total_images = count( $images );
    						$image = array_shift( $images );
    						$image_img_tag = wp_get_attachment_image( $image->ID, 'thumbnail' );
    				?>
    						<div class="gallery-thumb">
    							<a class="size-thumbnail" href="<?php the_permalink(); ?>"><?php echo $image_img_tag; ?></a>
    						</div><!-- .gallery-thumb -->
    						<p><em><?php printf( __( 'This gallery contains <a %1$s>%2$s photos</a>.', 'twentyten' ),
    								'href="' . get_permalink() . '" title="' . sprintf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ) . '" rel="bookmark"',
    								$total_images
    							); ?></em></p>
    				<?php endif; ?>
    						<?php the_excerpt(); ?>
    <?php endif; ?>

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • やりたいことが具体的でないのですが、最初の1枚でいいとか、画像が無かったらどうするとか。
    それはさておきならば。

    <?php
    $files = get_children('post_parent=' . $post->ID . '&post_type=attachment&post_mime_type=image/jpeg');
    $keys = array_keys($files);
    $num = $keys[0];
    $img_url = wp_get_attachment_image($num, 'medium');
    ?>

    こんなんとか?

    最初の
    $files = get_children('post_parent=' . $post->ID . '&post_type=attachment&post_mime_type=image/jpeg');
    だけわかれば、あとは簡単っぽいです。

    トピック投稿者 spaci654

    (@spaci654)

    >kvexさま

    ありがとうございます。実現できました!
    ちなみに自分はループ内にこう書きました。
    同じ疑問をお持ちの方のお役に立てますように。。。

    <h2 class="posttitle"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    
    <?php
    $files = get_children('post_parent=' . $post->ID . '&post_type=attachment&post_mime_type=image/jpeg');
    $keys = array_keys($files);
    $num = $keys[0];
    $img_url = wp_get_attachment_image($num, 'medium');
    ?>
    
    <?php echo $img_url; ?>
    
    <?php the_excerpt(); ?>
    <a href="<?php the_permalink(); ?>">詳しく見る</a>
2件の返信を表示中 - 1 - 2件目 (全2件中)
  • トピック「記事に埋め込んだギャラリーの中から1枚を抜き出してアーカイブに表示」には新たに返信することはできません。