Advanced Custom Fieldを導入
Advanced Custom Fieldをして有効化しておきます。
フィールドを追加
- フィールド名を決める
- フィールドタイプを決める
- 返り値はID
- 投稿ルールを追加
ACFより新規追加
- フィールドグループ名 メイン画像登録エリア
- フィールド名 main_image とする
- フィールドタイプは画像
- 返り値はID
- 投稿ルールを追加(例として固定ページと地域貢献活動の追加)
カスタムフィールドを呼び出す
if (is_page() || is_singular('daily_contribution')) {
$attachment_id = get_field('main_img');
if(is_page()):
return wp_get_attachment_image($attachment_id, 'top');
else:
return wp_get_attachment_image($attachment_id, 'detail');
endif;
}
get_field():カスタムフィールドを呼び出す
wp_get_attachment_image():
ACFより新規追加
- フィールドグループ名 イベント種類拡張エリア
- フィールド名 event_image とする
- フィールドタイプは画像
- 返り値はID
- 投稿ルールを追加(タクソノミー イベントの種類)追加指定タクソノミーを選択する
$term_obj = get_queried_object();
$image_id = get_field('event_img', $term_obj->taxonomy. '_'. $term_obj->term_id);
return wp_get_attachment_image($image_id, 'detail');
$term_obj = get_queried_object();
get_queried_object()
関数: 現在表示されているページに関連するオブジェクト(投稿、ページ、用語など)を取得します。この場合、用語オブジェクトを取得していると考えられます。$term_obj
変数: 取得した用語オブジェクトを格納します。この変数には、用語のID、スラッグ、名前などの情報が含まれています。
$image_id = get_field('event_img', $term_obj->taxonomy. '_'. $term_obj->term_id);
get_field
関数: Advanced Custom Fields(ACF)プラグインの関数で、カスタムフィールドの値を取得します。'event_img'
: 画像のIDが保存されているカスタムフィールドの名前です。$term_obj->taxonomy. '_'. $term_obj->term_id
: 用語のユニークな識別子を作成します。用語の分類(タクソノミー)と用語のIDを組み合わせることで、どの用語の画像を取得するかを特定します。
return wp_get_attachment_image($image_id, 'detail');
wp_get_attachment_image
関数: 画像の添付ファイルIDから、HTML形式の画像タグを生成します。$image_id
: 2.で取得した画像のID。'detail'
: 画像のサイズ。WordPressの設定で事前に定義された画像サイズです。
ACFで新規追加する
今回は店舗1,店舗2の2つのフィールドグループを作成
今回は店舗1,店舗2の2つのフィールドグループを作成
- フィールドグループ名 店舗1の詳細、店舗2の詳細
- フィールド名 first_shop_detailとする
- フィールドタイプはgroup
- 投稿ルールを追加(ページテンプレート 店舗詳細)追加指定タクソノミーを選択する
- レイアウト 行
- サブフィールドを追加(詳細は下記)
サブフィールドを追加
追加指定項目を追加する。
追加指定項目を追加する。
- 店舗名 shop_name
- 店舗画像 shop_img
- アピールポイント shop_strength
- 営業時間 shop_hours
- フロア情報 floor_info
//呼びたいところで
<?php
$shops = array('first_shop_detail', 'second_shop_detail');
foreach ($shops as $shop):
if (have_rows($shop)):
while (have_rows($shop)):
the_row();
get_template_part('content-shop-detail');
endwhile;
endif;
endforeach;
?>
//content-shop-detail.php
<li class="shopList-item">
<div class="shop-image">
<?php
$image_id = get_sub_field('shop_img');
echo wp_get_attachment_image($image_id, 'shop_detail');
?>
</div>
<div class="shop-body">
<p class="shop-title"><?php the_sub_field('shop_name') ?> </p>
<p class="shop-caption">
<?php the_sub_field('shop_strength'); ?>
</p>
<div class="shop-detail">
<dl>
<dt>営業時間</dt>
<dd> <?php the_sub_field('shop_hours'); ?>
</dd>
</dl>
<dl>
<dt>フロア情報</dt>
<dd> <?php the_sub_field('floor_info'); ?>
</dd>
</dl>
</div>
</div>
</li>
ACFで新規追加
- フィールドグループ名 英語タイトル
- フィールド名 english_titleとする
- フィールドタイプはテキスト
- 投稿ルールを追加(固定ページ、カスタム投稿、カテゴリ、カスタムタクソノミー)追加指定したいページ、タクソノミーを選択する
//英語タイトルを得るための関数を作成
function get_main_en_title() {
if ( is_category() ):
// カテゴリページでtermオブジェクトを得る
$term_obj = get_queried_object();
$english_title = get_field( 'english_title', $term_obj->taxonomy. '_'. $term_obj->term_id );
return $english_title;
elseif ( is_singular( 'post' ) ):
$term_obj = get_the_category();
$english_title = get_field( 'english_title', $term_obj[0]->taxonomy. '_'. $term_obj[0]->term_id );
return $english_title;
elseif ( is_page() || is_singular( 'daily_contribution' ) ):
return get_field( 'english_title' );
elseif ( is_search() ):
return 'Search Result';
elseif ( is_404() ):
return '404 Not Found';
elseif ( is_tax() ):
$term_obj = get_queried_object();
$english_title = get_field( 'english_title', $term_obj->taxonomy. '_'. $term_obj->term_id );
return $english_title;
endif;
}