サポート » 使い方全般 » 特定のポストタイプに属する記事一覧にカスタムフィールドの画像を表示

  • 解決済 hachinicov

    (@hachinicov)


    はじめまして、ご閲覧ありがとうございます。
    Wordpress・phpともに不慣れなため、お知恵をお貸し頂ければ幸いです。

    index.phpにてカスタム投稿タイプ名「article」の新着記事4件の表示を行おうとしています。その際、それらの記事に登録してあるカスタムフィールドの画像も表示しようとしたところ、上手く表示がされません。

    現状:top_banner_img(画像)があればtop_banner_imgを表示、ない場合はアイキャッチ画像(サムネイル)を表示→top_banner_imgの登録がある記事もアイキャッチ画像(サムネイル)が表示されてしまう
    参照したサイト:
    http://webcre-archive.com/2012/09/11/advanced-custom-fields-image/
    http://www.advancedcustomfields.com/resources/functions/get_field/

    【Wordpress】3.5.1
    【表示したいページ】index.php
    【プラグイン】Advanced Custom Fields
    【カスタム投稿タイプ名】article
    【フィールド名】top_banner_img
    【返り値】画像ID

    <?php
    $args = array(
    	'post_type' => 'article',
    	'showposts' => 4,
    	);
    ?>
    <?php $the_query = new WP_Query( $args ); ?>
    <?php if($the_query->have_posts()): ?>
    	<ul class="newlistDouble">
    	<?php if ($the_query->have_posts()):while($the_query->have_posts()):$the_query->the_post(); ?>
    	<li><a href="<?php the_permalink(); ?>">
    
    	<?php if( get_field('top_banner_img') ): ?>
    	<?php
    		$attachment_id = get_field('top_banner_img');
    		$size = "full"; // (thumbnail, medium, large, full or custom size)
    		$image = wp_get_attachment_image_src( $attachment_id, $size );
    		$attachment = get_post( get_field('top_banner_img') );
    		$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
    		$image_title = $attachment->post_title;
    	?>
    	<img src="<?php echo $image[0]; ?>" alt="<?php echo $alt; ?>" title="<?php echo $image_title; ?>" />
    	<?php else : ?>
    	<?php the_post_thumbnail(array(115,115)); ?>
    	<?php endif; ?>
    
    	</a><span><a href="<?php the_permalink(); ?>"><?php echo mb_substr($post->post_title, 0, 25).'...'; ?></a></span><?php echo mb_substr(get_the_excerpt(), 0, 50).'...'; ?></li>
    	<?php endwhile; endif; ?></ul>
    <?php endif; ?>
    <?php wp_reset_query(); ?>

    この他、

    <?php
    	$attachment_obj = get_field('top_banner_img',$post->ID);
    	echo wp_get_attachment_image( $attachment_obj->ID, "full" );
    	?>

    や、カスタムフィールドの返り血を画像オブジェクトに変更し

    <?php echo the_field("top_banner_img","article"); ?>

    なども試したのですが上手くいきません。

    かれこれ何日も手詰まりの状態です…
    ご教授いただけましたら幸いです。
    どうぞよろしくお願いいたします。

7件の返信を表示中 - 1 - 7件目 (全7件中)
  • このコードは、どこに書いているのですか?本当に index.php ですか?sidebar.php とかじゃないですか?

    index.php なら、きちんとポストIDが入っているか調べてみてください。

    echo $post->ID;
    $attachment_id = get_field('top_banner_img',$post->ID);
    echo $attachment_id;

    トピック投稿者 hachinicov

    (@hachinicov)

    ご回答ありがとうございます。
    時間が空いてしまい申し訳ありません。

    どこに書いているのですか?本当に index.php ですか?

    index.phpになります。

    <?php
    $args = array(
    	'post_type' => 'article',
    	'showposts' => 4,
    	);
    ?>
    <?php $the_query = new WP_Query( $args ); ?>
    <?php if($the_query->have_posts()): ?>
    	<ul class="newlistDouble">
    	<?php if ($the_query->have_posts()):while($the_query->have_posts()):$the_query->the_post(); ?>
    	<li><a href="<?php the_permalink(); ?>">
    
    	<?php echo $post->ID;
    	$attachment_id = get_field('top_banner_img',$post->ID);
    	echo $attachment_id; ?>
    
    	</a><span><a href="<?php the_permalink(); ?>"><?php echo mb_substr($post->post_title, 0, 25).'...'; ?></a></span><?php echo mb_substr(get_the_excerpt(), 0, 50).'...'; ?></li>
    	<?php endwhile; endif; ?></ul>
    <?php endif; ?>
    <?php wp_reset_query(); ?>

    頂きましたコードを上記のように致しましたところ、ポストIDらしき数字が表示されました。
    しかしtop_banner_imgの値は表示されていないようでしたので、頂いたコードを分割して表示を行ってみました。

    【1】表示結果→数字

    echo $post->ID;

    【2】表示結果→なし

    $attachment_id = get_field('top_banner_img',$post->ID);
    echo $attachment_id;

    となり、idは入っているようなのですが、フィールドの値を受け取れていないようです…

    引き続きお力を貸して頂けると幸いです。
    よろしくお願い致します。

    トピック投稿者 hachinicov

    (@hachinicov)

    たびたび申し訳ありません…一番最初に書いたコードで表示が行えました (´;ω;`)
    Advanced Custom Fieldsの設定をいじっている間に、登録していた画像が設定から外れていたようです。
    お騒がせいたしました。
    pluto1234さん、誠にありがとうございました!

    一応コードを置いておきますので、どなたかの参考になれば幸いです。

    <?php
    $args = array(
    	'post_type' => 'article',
    	'showposts' => 4,
    	);
    ?>
    <?php $the_query = new WP_Query( $args ); ?>
    <?php if($the_query->have_posts()): ?>
    	<ul class="newlistDouble">
    	<?php if ($the_query->have_posts()):while($the_query->have_posts()):$the_query->the_post(); ?>
    
    <li><a>">
    	<?php if( get_field('top_banner_img') ): ?>
    	<?php
    		$attachment_id = get_field('top_banner_img');
    		$size = "full"; // (thumbnail, medium, large, full or custom size)
    		$image = wp_get_attachment_image_src( $attachment_id, $size );
    		$attachment = get_post( get_field('top_banner_img') );
    		$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
    		$image_title = $attachment->post_title;
    	?>
    	<img src="<?php echo $image[0]; ?>" alt="<?php echo $alt; ?>" title="<?php echo $image_title; ?>" />
    	<?php else : ?>
    	<?php the_post_thumbnail(array(115,115)); ?>
    	<?php endif; ?>
    
    	</a><span><a>"><?php echo mb_substr($post->post_title, 0, 25).'...'; ?></a></span><?php echo mb_substr(get_the_excerpt(), 0, 50).'...'; ?></li>
    	<?php endwhile; endif; ?>
    <?php endif; ?>
    <?php wp_reset_query(); ?>

    その他、幾つか気になったので書いておきます。
    1) if($the_query->have_posts()): が重なっています。
    2) 閉じる方の /ul が足りません。
    3) 最後は、wp_reset_postdata(); で十分です($wp_query は破壊していないので)。

    <?php
    $args = array(
    	'post_type' => 'article',
    	'showposts' => 4,
    	);
    ?>
    <?php $the_query = new WP_Query( $args ); ?>
    <?php if($the_query->have_posts()): ?>
    	<ul class="newlistDouble">
    	<?php while($the_query->have_posts()):$the_query->the_post(); ?>
    
    <li><a>">
    	<?php if( get_field('top_banner_img') ): ?>
    	<?php
    		$attachment_id = get_field('top_banner_img');
    		$size = "full"; // (thumbnail, medium, large, full or custom size)
    		$image = wp_get_attachment_image_src( $attachment_id, $size );
    		$attachment = get_post( get_field('top_banner_img') );
    		$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
    		$image_title = $attachment->post_title;
    	?>
    	<img src="<?php echo $image[0]; ?>" alt="<?php echo $alt; ?>" title="<?php echo $image_title; ?>" />
    	<?php else : ?>
    	<?php the_post_thumbnail(array(115,115)); ?>
    	<?php endif; ?>
    
    	</a><span><a>"><?php echo mb_substr($post->post_title, 0, 25).'...'; ?></a></span><?php echo mb_substr(get_the_excerpt(), 0, 50).'...'; ?></li>
    	<?php endwhile;  ?>
            </ul>
    <?php endif; ?>
    <?php wp_reset_postdata(); ?>

    ※追加:get_field(‘top_banner_img’) を何度も呼び出していますが、1回呼び出せばいいはずです。

    トピック投稿者 hachinicov

    (@hachinicov)

    何度もすみません、ご丁寧にありがとうございます!
    ミスが多すぎて大変お恥ずかしいです…

    厚かましくもさらにお尋ねさせていただきたいのですが、

    ※追加:get_field(‘top_banner_img’) を何度も呼び出していますが、1回呼び出せばいいはずです。

    は以下のように変数に置き換えれば良い。という意味合いでしょうか?(以下でも動作しました)
    もしよろしければご回答頂ければ幸いです。

    <?php
    	$args = array(
    		'post_type' => 'article',
    		'showposts' => 4,
    		);
    	?>
    	<?php $the_query = new WP_Query( $args ); ?>
    	<?php if($the_query->have_posts()): ?>
    	<ul class="newlistDouble">
    	<?php while($the_query->have_posts()):$the_query->the_post(); ?>
    	<li><a href="<?php the_permalink(); ?>">
    	<?php if( $attachment_id = get_field('top_banner_img') ): ?>
    	<?php
    		$attachment = get_post( $attachment_id );
    		$size = "full"; // (thumbnail, medium, large, full or custom size)
    		$image = wp_get_attachment_image_src( $attachment_id, $size );
    		$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
    		$image_title = $attachment->post_title;
    	?>
    	<img src="<?php echo $image[0]; ?>" alt="<?php echo $alt; ?>" title="<?php echo $image_title; ?>" />
    	<?php else : ?>
    	<?php the_post_thumbnail(array(115,115)); ?>
    	<?php endif; ?>
    
    	</a><span><a href="<?php the_permalink(); ?>"><?php echo mb_substr($post->post_title, 0, 25).'...'; ?></a></span><?php echo mb_substr(get_the_excerpt(), 0, 50).'...'; ?></li>
    	<?php endwhile;  ?>
            </ul>
    	<?php endif; ?>
    	<?php wp_reset_postdata(); ?>

    >以下のように変数に置き換えれば良い。という意味合いでしょうか?
    そうです。
    今の時代はコンピュータは高速で、特に Web の場合は、PHP をちょっと変更ところで、全体的なパフォーマンスに影響はありませんが・・。1マイクロ秒くらいは速くなるかもしれません。

    トピック投稿者 hachinicov

    (@hachinicov)

    ご回答ありがとうございます!

    なるほど、パフォーマンスの上で変わってくるのですね。
    考えがそこまで至っていなかったので、とても勉強になりました。
    これを機に、もう一歩踏み込んでphpを学びたいと思います。

    この度は貴重な時間を割いて頂き、まことにありがとうございました。

7件の返信を表示中 - 1 - 7件目 (全7件中)
  • トピック「特定のポストタイプに属する記事一覧にカスタムフィールドの画像を表示」には新たに返信することはできません。