2021/04/28
Wordpress /
カスタムフィールド(Advanced Custom Fields)

出力方法
下記で登録したカスタムフィールドを呼び出せる
get_field($selector, [$post_id], [$format_value]);
$selector | string | 必須 | フィールド名 |
$post_id | mixed | オプション | 投稿ID。デフォルトで現在の投稿ID |
$format_value | bool | オプション | フォーマットロジックを適用するかどうか。デフォルトはtrue |
下記でスラッグから投稿のIDを取得できる。別のページのカスタムフィールドを呼び出したい時に使用できる。
$get_page_id = get_page_by_path("top");//topは固定ページのスラッグ名
$get_page_id = $get_page_id->ID;
アイキャッチ画像を任意の複数個を指定できるようにする方法
catching1,catching2,catching3…のようなフィールド名でフィールドを作成した場合。
<?php $i = 1; while($catching = get_field('catching' . $i)): ?>
<div class="swiper-slide">
<img src="<?php echo $catching['url'] ?>" alt="<?php echo $catching['title'] ?>">
</div>
<?php $i++; endwhile; ?>