🏡まったのブログ

ACFの出力方法

テキスト

<?php if (get_field('text_field_name')) {												 
    echo the_field('text_field_name');
} ?>

通常のループの中ではない場所で出力したい場合

通常のループの中ではない場所で出力したい場合は投稿IDが必要

get_field($selector, [$post_id], [$format_value]);

$selector (string) (Required) The field name or field key.

$post_id (mixed) (Optional) The post ID where the value is saved. Defaults to the current post.

$format_value (bool) (Optional) Whether to apply formatting logic. Defaults to true.

画像

返り値のフォーマットがURLの場合

追記予定

返り値のフォーマットが画像IDの場合

<?php
    $img = get_field('image_field_name');
    $img_urls = wp_get_attachment_image_src($img, 'full');
    if ($img_urls) { ?>
        <img src="<?php echo $img_urls[0]; ?>" alt="">
    <?php } 
?>

ただしレスポンシブ画像としての出力はしてくれない。

(thumbnail, medium, large, full)または2つのアイテムを持つ配列が幅と高さをピクセルで、array(32,32)という形で表します

https://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/wp_get_attachment_image_src#.E3.83.91.E3.83.A9.E3.83.A1.E3.83.BC.E3.82.BF

array指定が効いてくれなかったのは、元のアスペクト比と違う比率を指定したからか?