サイト制作・WEBコンサルティング

WordPressプラグイン紹介 コンテンツ管理に必須「 Advanced Custom Fields 」:後編

WordPressプラグイン紹介 コンテンツ管理に必須「 Advanced Custom Fields 」

前回に引き続き『 Advanced Custom Fields 』プラグインをご紹介します。
前回は「Advanced Custom Fields」そのものの利用シーンと活用事例を紹介しました。
今回は、おもに管理画面から入力した各フィールドのサイトへの出力を中心に解説します。

このプラグイン、入力フィールドを管理画面に呼び出してくれるのはうれしいところですが、サイトへの出力については、多少なりともWordPressタグについての専門知識が必要になります。

WordPressタグとはWordPress内部であらかじめ用意された記事情報を出力させるための記述と理解してよいかと思います。

カスタムフィールドの出力

出力の基本

例えば、一般的な出力方法は以下の通りとなります。

都道府県 : <?php echo get_post_meta( post->ID, '都道府県', true ); ?>

上記はフィールド名が「都道府県」である場合の出力例です。
上記のタグを記事の出力ループにおけば出力されます。

ただこれだけだと、仮に記事の編集で「都道府県」が未入力だった場合に、

都道府県 : [空欄]

となってしまいます。
そこで入力がないときの出力についても考えましょう。
以下、サンプルソースです。

<?php $prefecture = get_post_meta( post->ID, '都道府県', true ); ?>
<?php if( $prefecture ): ?>
都道府県 : <?php echo get_post_meta( post->ID, '都道府県', true ); ?>
<?php else ?>
都道府県 : -
<?php endif; ?>

各フィールドごとの出力例

では上記の基本を踏まえたうえで各フィールド種類ごとの出力例を以下に紹介します。

テキストフィールドの場合

<dl class="cfield-dl">
<?php $cfield = get_post_meta($post->ID, 'cfield_text', true); ?>
<dt>テキストフィールド</dt>
<?php if($cfield): /* 入力がある場合 */ ?>
<dd><?php echo esc_html($cfield); ?></dd>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
</dl>

テキストエリアの場合

<dl class="cfield-dl">
<?php $cfield = get_post_meta($post->ID, 'cfield_textarea', true); ?>
<dt>テキストエリア</dt>
<?php if($cfield): /* 入力がある場合 */ ?>
<dd><?php echo esc_html($cfield); ?></dd>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
</dl>

数値フィールドの場合

※ 数値フィールドの例 数字のカンマ「,」をとる場合はnumber_format($cfield) → $cfield とします。

<dl class="cfield-dl">
<?php $cfield = get_post_meta($post->ID, 'cfield_suchi', true); ?>
<dt>数値</dt>
<?php if($cfield): /* 入力がある場合 */ ?>
<dd><?php echo esc_html(number_format($cfield)); ?></dd>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
</dl>

メールアドレスの場合

<dl class="cfield-dl">
<?php /* メール */ ?>
<?php $cfield = get_post_meta($post->ID, 'cfield_mail', true); ?>
<dt>メール</dt>
<?php if($cfield): /* 入力がある場合 */ ?>
<dd><a href="mailto:<?php echo esc_html($cfield); ?>"><?php echo esc_html($cfield); ?></a></dd>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
</dl>

パスワードの場合

<dl class="cfield-dl">
<?php $cfield = get_post_meta($post->ID, 'cfield_password', true); ?>
<dt>パスワード</dt>
<?php if($cfield): /* 入力がある場合 */ ?>
<dd><?php echo esc_html($cfield); ?></dd>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
</dl>

Wysiwygエディタの例

<dl class="cfield-dl">
<?php $cfield = get_post_meta($post->ID, 'cfield_wysiwyg', true); ?>
<dt>Wysiwygエディタ</dt>
<?php if($cfield): /* 入力がある場合 */ ?>
<dd><?php echo $cfield; ?></dd>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
</dl>

画像の場合

<dl class="cfield-dl">
<?php
// カスタムフィールドから画像IDを取得
$img_id = get_post_meta($post->ID, 'cfield_image', true);
// 画像IDからイメージ画像取得( 第2引数 : thumbnail or medium or large or full )
$img = wp_get_attachment_image_src($img_id, 'thumbnail');
// 画像IDから画像のメタ情報を取得
$attachment = get_post($img_id);
// 画像alt取得
$img_alt = get_post_meta($img_id, '_wp_attachment_image_alt', true);
// 画像キャプション取得
$img_caption = $attachment->post_excerpt;
// 画像説明を取得
$img_explain = $attachment->post_content;
?>
<dt>画像</dt>
<?php if($img): /* 入力がある場合 */ ?>
<dd class="cfield-dd-has-child">
↓↓ 内容の詳細 ↓↓
<dl>
<dt>サムネイル画像</dt>
<dd> <img src="<?php echo esc_url($img[0]); ?>" width="<?php echo esc_attr($img[1]); ?>" width="<?php echo esc_attr($img[2]); ?>" alt="<?php echo esc_attr($img_alt); ?>"></dd>
<dt>キャプション</dt>
<dd>: <?php echo esc_html($img_caption); ?></dd>
<dt>説明</dt>
<dd>: <?php echo esc_html($img_explain); ?></dd>
</dl>
</dd>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
</dl>

ファイルの場合

<dl class="cfield-dl">
<?php
// ファイルIDを取得
$file_id = get_post_meta($post->ID, 'cfield_file', true);
// ファイルIDから情報を取得
$file = get_post($file_id);
?>
<dt>ファイル</dt>
<?php if($file): /* 入力がある場合 */ ?>
<dd class="cfield-dd-has-child">
↓↓ 内容の詳細 ↓↓
<dl>
<dt>ファイルURL</dt>
<dd><a href="<?php echo esc_url($file->guid); ?>"><?php echo esc_url($file->guid); ?></a></dd>
<dt>ファイル名</dt>
<dd><?php echo esc_html($file->post_title); ?></dd>
<dt>投稿日</dt>
<dd><?php echo esc_html($file->post_date); ?></dd>
<dt>説明</dt>
<dd><?php echo esc_html($file->post_content); ?></dd>
<dt>キャプション</dt>
<dd><?php echo esc_html($file->post_excerpt); ?></dd>
</dl>
</dd>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
</dl>

セレクトボックスの場合

<dl class="cfield-dl">
<?php $cfield = get_post_meta($post->ID, 'cfield_selectbox', true); ?>
<dt>セレクトボックス</dt>
<?php if($cfield): /* 入力がある場合 */ ?>
<dd><?php echo esc_html($cfield); ?></dd>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
</dl>

チェックボックスの場合

<dl class="cfield-dl">
<?php
$cfields = get_post_meta($post->ID, 'cfield_checkbox', true);
?>
<dt>チェックボックス</dt>
<?php if($cfields): /* 入力がある場合 */ ?>
<dd class="cfield-dd-has-child">
↓↓ 内容の詳細 ↓↓
<?php foreach ($cfields as $cfield): ?>
<?php echo esc_html($cfield); ?> /
<?php endforeach; ?>
</dd>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
</dt>
</dl>

ラジオボタンの例

<dl class="cfield-dl">
<?php /* ラジオボタン */ ?>
<?php $cfield = get_post_meta($post->ID, 'cfield_radio', true); ?>
<dt>ラジオボタン</dt>
<?php if($cfield): /* 入力がある場合 */ ?>
<dd><?php echo esc_html($cfield); ?></dd>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
</dl>

ページリンクの場合

<dl class="cfield-dl">
<?php
$post_id = get_post_meta($post->ID, 'cfield_pagelink', true);
$get_post = get_post($post_id);
?>
<dt>ページリンク</dt>
<?php if($post_id): /* 入力がある場合 */ ?>
<dd class="cfield-dd-has-child">
↓↓ 内容の詳細 ↓↓
<dl>
<dt>投稿タイトル</dt>
<dd><?php echo esc_html($get_post->post_title); ?></dd>
<dt>投稿日</dt>
<dd><?php echo esc_html($get_post->post_date); ?></dd>
<dt>投稿URL</dt>
<dd><a href="<?php echo esc_url($get_post->guid); ?>"><?php echo esc_url($get_post->guid); ?></a></dd>
<dt>投稿の抜粋</dt>
<dd><?php echo esc_html($get_post->post_excerpt); ?></dd>
<dt>投稿の内容</dt>
<dd><?php echo esc_html($get_post->post_content); ?></dd>
</dl>
</dd>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
</dl>

投稿オブジェクトの場合

<dl class="cfield-dl">
<?php
$post_id = get_post_meta($post->ID, 'cfield_object', true);
$get_post = get_post($post_id);
?>
<dt>投稿オブジェクトの例</dt>
<?php if($post_id): /* 入力がある場合 */ ?>
<dd class="cfield-dd-has-child">
↓↓ 内容の詳細 ↓↓
<dl>
<dt>投稿タイトル</dt>
<dd><?php echo esc_html($get_post->post_title); ?></dd>
<dt>投稿日</dt>
<dd><?php echo esc_html($get_post->post_date); ?></dd>
<dt>投稿URL</dt>
<dd><a href="<?php echo esc_url($get_post->guid); ?>"><?php echo esc_url($get_post->guid); ?></a></dd>
<dt>投稿の抜粋</dt>
<dd><?php echo esc_html($get_post->post_excerpt); ?></dd>
<dt>投稿の内容</dt>
<dd><?php echo esc_html($get_post->post_content); ?></dd>
</dl>
</dd>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
</dl>

関連の場合

<dl class="cfield-dl">
<?php $post_ids = get_post_meta($post->ID, 'cfield_kanren', true); ?>
<dt>関連</dt>
<?php foreach ($post_ids as $post_id):
$get_post = get_post($post_id); ?>
<?php if($post_id): /* 入力がある場合 */ ?>
<dd class="cfield-dd-has-child">
↓↓ 内容の詳細 ↓↓
<dl>
<dt>投稿タイトル</dt>
<dd><?php echo esc_html($get_post->post_title); ?></dd>
<dt>投稿日</dt>
<dd><?php echo esc_html($get_post->post_date); ?></dd>
<dt>投稿URL</dt>
<dd><a href="<?php echo esc_url($get_post->guid); ?>"><?php echo esc_url($get_post->guid); ?></a></dd>
<dt>投稿の抜粋</dt>
<dd><?php echo esc_html($get_post->post_excerpt); ?></dd>
<dt>投稿の内容</dt>
<dd><?php echo esc_html($get_post->post_content); ?></dd>
</dl>
</dd>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
<?php endforeach; ?>
</dl>

タクソノミーの場合

<dl class="cfield-dl">
<?php /* タクソノミー */ ?>
<?php $tax_id = get_post_meta($post->ID, 'cfield_taxonomy', true); ?>
<dt>タクソノミー</dt>
<?php if($tax_id): /* 入力がある場合 */ ?>
<?php $term = get_term($tax_id, 'category'); ?>
<dd class="cfield-dd-has-child">
↓↓ 内容の詳細 ↓↓
<dl>
<dt>タームID</dt>
<dd><?php echo esc_html($term->ID); ?></dd>
<dt>ラベル名</dt>
<dd><?php echo esc_html($term->name); ?></dd>
<dt>スラッグ名</dt>
<dd><?php echo esc_html($term->slug); ?></dd>
</dl>
</dd>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
</dl>

ユーザーIDの場合

<dl class="cfield-dl">
<?php /* ユーザー */ ?>
<?php $user_id = get_post_meta($post->ID, 'cfield_user', true); ?>
<dt>ユーザーID</dt>
<?php if($user_id): /* 入力がある場合 */ ?>
<dd><?php echo esc_html($user_id); ?></dd>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
<?php /* Google Map */ ?>
<?php $cfield = get_post_meta($post->ID, 'cfield_google_map', true); ?>
</dl>

Google Mapの場合

HTML側
<p class="cfield-explain">
Google Mapの例<br>
※ 緯度と経度だけが取得できます。<br>
※ 地図を表示するには別途そのための実装が必要です。
</p>
<dl class="cfield-dl">
<dt>Google Map</dt>
<?php if($cfield): /* 入力がある場合 */ ?>
<?php foreach ($post_ids as $post_id): ?>
<dd class="cfield-dd-has-child">
↓↓ 内容の詳細 ↓↓
<dl>
<dt>アドレス</dt>
<dd><?php echo esc_html($cfield['address']); ?></dd>
<dt>緯度</dt>
<dd><?php echo esc_html($cfield['lat']); ?></dd>
<dt>経度</dt>
<dd><?php echo esc_html($cfield['lng']); ?></dd>
<dt>地図</dt>
<dd>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $cfield['lat']; ?>" data-lng="<?php echo $cfield['lng']; ?>"></div>
</div>
</dd>
</dl>
</dd>
<?php endforeach; ?>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
</dl>
functions.php
/*
* acfでgoogle mapでエラーがでるのでそれを修正
*/
add_filter('acf/fields/google_map/api', function () {
  return array(
    'libraries' => 'places',
    'key' => 'AIzaSyDb-v5UHEHc4LED8Hm8VEylEowICcip5ag', // Googleで発行したキーを用意する
    'client' => ''
  );
});

// google map api
add_action('wp_enqueue_scripts', function(){
  $api_key = 'AIzaSyDb-v5UHEHc4LED8Hm8VEylEowICcip5ag';
  wp_enqueue_script('google map', 'https://maps.googleapis.com/maps/api/js?key=' . $api_key);
});
jQuery ( JavaScirpt ) ファイル
(function($) {

/*
* new_map
*
* This function will render a Google Map onto the selected jQuery element
*
* @type function
* @date 8/11/2013
* @since 4.3.0
*
* @param $el (jQuery element)
* @return n/a
*/
function new_map( $el ) {
// var
var $markers = $el.find('.marker');
// vars
var args = {
zoom : 16,
center : new google.maps.LatLng(0, 0),
mapTypeId : google.maps.MapTypeId.ROADMAP
};
// create map
var map = new google.maps.Map( $el[0], args);
// add a markers reference
map.markers = [];
// add markers
$markers.each(function(){
add_marker( $(this), map );
});
// center map
center_map( map );
// return
return map;
}

/*
* add_marker
*
* This function will add a marker to the selected Google Map
*
* @type function
* @date 8/11/2013
* @since 4.3.0
*
* @param $marker (jQuery element)
* @param map (Google Map object)
* @return n/a
*/

function add_marker( $marker, map ) {
// var
var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );
// create marker
var marker = new google.maps.Marker({
position : latlng,
map : map
});
// add to array
map.markers.push( marker );
// if marker contains HTML, add it to an infoWindow
if( $marker.html() ) {
// create info window
var infowindow = new google.maps.InfoWindow({
content : $marker.html()
});
// show info window when marker is clicked
google.maps.event.addListener(marker, 'click', function() {
infowindow.open( map, marker );
});
}
}

/*
* center_map
*
* This function will center the map, showing all markers attached to this map
*
* @type function
* @date 8/11/2013
* @since 4.3.0
*
* @param map (Google Map object)
* @return n/a
*/

function center_map( map ) {
// vars
var bounds = new google.maps.LatLngBounds();
// loop through all markers and create bounds
$.each( map.markers, function( i, marker ){
var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );
bounds.extend( latlng );
});

// only 1 marker?
if( map.markers.length == 1 ) {
// set center of map
map.setCenter( bounds.getCenter() );
map.setZoom( 16 );
} else {
// fit to bounds
map.fitBounds( bounds );
}
}

/*
* document ready
*
* This function will render each map when the document is ready (page has loaded)
*
* @type function
* @date 8/11/2013
* @since 5.0.0
*
* @param n/a
* @return n/a
*/
// global var
var map = null;

$(document).ready(function(){
$('.acf-map').each(function(){
// create map
map = new_map( $(this) );
});
});
})(jQuery);
css
.acf-map {
width: 100%;
height: 400px;
border: #ccc solid 1px;
margin: 20px 0;
}

/* fixes potential theme css conflict */
.acf-map img {
max-width: inherit !important;
}

デイトピッカーの場合

<p class="cfield-explain">
デイトピッカーの例
</p>
<dl class="cfield-dl">
<?php /* デイトピッカー */ ?>
<?php $cfield = get_post_meta($post->ID, 'cfield_datepicker', true); ?>
<dt>デイトピッカー</dt>
<?php if($cfield): /* 入力がある場合 */ ?>
<dd><?php echo esc_html($cfield); ?></dd>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
</dl>

カラーピッカーの場合

<p class="cfield-explain">
カラーピッカーの例
</p>
<dl class="cfield-dl">
<?php /* カラーピッカー */ ?>
<?php $cfield = get_post_meta($post->ID, 'cfield_colorpicker', true); ?>
<dt>カラーピッカー</dt>
<?php if($cfield): /* 入力がある場合 */ ?>
<dd><?php echo esc_html($cfield); ?></dd>
<?php else: /* 入力がない場合 */ ?>
<dd> - </dd>
<?php endif; ?>
</dl>

次回は、高機能カレンダープラグイン『Event Organiser』プラグインについてお届けいたします。

おたのしみに。

たけぼーでした!

お電話でのご相談はこちら(平日9時~18時)