Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save senooat/a5a0fda33838a258431bb4a95780bb69 to your computer and use it in GitHub Desktop.
Save senooat/a5a0fda33838a258431bb4a95780bb69 to your computer and use it in GitHub Desktop.
『Advanced Custom Fields』の各フィールドの出力方法について
/*-----------------------------------
フィールドタイプ:基本
-----------------------------------*/
/*テキスト-----------*/
<?php if( get_field('フィールド名') ): ?>
<?php the_field('フィールド名'); ?>
<?php endif; ?>
/*テキストエリア-----------*/
<?php if( get_field('フィールド名') ): ?>
<?php the_field('フィールド名'); ?>
<?php endif; ?>
/*数値-----------*/
<?php if( get_field('フィールド名') ): ?>
<?php the_field('フィールド名'); ?>
<?php endif; ?>
/*メール-----------*/
<?php if( get_field('フィールド名') ): ?>
<?php the_field('フィールド名'); ?>
<?php endif; ?>
/*パスワード-----------*/
<?php if( get_field('フィールド名') ): ?>
<?php the_field('フィールド名'); ?>
<?php endif; ?>
/*-----------------------------------
フィールドタイプ:content
-----------------------------------*/
/*Wysiwyg エディタ-----------*/
<?php if( get_field('フィールド名') ): ?>
<?php the_field('フィールド名'); ?>
<?php endif; ?>
/*画像(返り値が「画像ID」の場合)-----------*/
<?php if( get_field('フィールド名') ): ?>
<?php $thumb = wp_get_attachment_image_src( get_field('フィールド名') , 'full' ); //thumbnail, medium, large, full ?>
<img src="<?php echo $thumb[0]; ?>" width="<?php echo $thumb[1]; ?>" height="<?php echo $thumb[2]; ?>" alt="">
<?php endif; ?>
/*画像(返り値が「画像URL」の場合)-----------*/
<?php if( get_field('フィールド名') ): ?>
<img src="<?php the_field('フィールド名'); ?>" alt="">
<?php endif; ?>
/*ファイル(返り値が「ファイルURL」の場合)-----------*/
<?php if( get_field('フィールド名') ): ?>
<a href="<?php the_field('フィールド名'); ?>" target="_blank">ファイル</a>
<?php endif; ?>
/*-----------------------------------
フィールドタイプ:選択肢
-----------------------------------*/
/*セレクトボックス-----------*/
<?php if( get_field('フィールド名') ): ?>
セレクトボックス:<?php the_field('フィールド名'); ?>
<?php endif; ?>
/*チェックボックス-----------*/
<?php if( get_field('フィールド名') ): ?>
<ul>
<?php $check = get_field('フィールド名');
foreach ( (array)$check as $value ): ?>
<li>チェックボックス:<?php echo $value; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
/*ラジオボタン-----------*/
<?php if( get_field('フィールド名') ): ?>
ラジオボタン:<?php the_field('フィールド名'); ?>
<?php endif; ?>
/*真/偽-----------*/
<?php if( get_field('フィールド名') ): ?>
true
<?php else: ?>
false
<?php endif; ?>
/*-----------------------------------
フィールドタイプ:Relational
-----------------------------------*/
/*ページリンク-----------*/
<?php if( get_field('フィールド名') ): ?>
<ul>
<?php $relational = get_field('フィールド名');
foreach ( (array)$relational as $value ): ?>
<li>ページリンク:<a href="<?php echo $value; ?>"><?php echo $value; ?></a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
/*投稿オブジェクト(複数の値を選択できるようにした場合)-----------*/
<?php $post_objects = get_field('post_obj');?>
<?php if( $post_objects ): ?>
<?php foreach( $post_objects as $post): ?>
<?php setup_postdata( $post ); ?>
<div>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<p><?php the_content(); ?></p>
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
/*タクソノミー(返り値が「Term Object」の場合)-----------*/
<?php if( get_field('フィールド名') ): ?>
<?php $term = get_field('フィールド名'); ?>
<p>タクソノミー:
<?php foreach ( (array)$term as $value ): ?>
<a href="<?php echo get_bloginfo('url') . '/archives/category/' . $value->slug; ?>"><?php echo $value->name; ?></a>
<?php if(next($term)!==FALSE){ echo ", ";} ?>
<?php endforeach; ?>
</p>
<?php endif; ?>
/*ユーザー(返り値が「セレクトボックス」の場合)-----------*/
<?php if( get_field('フィールド名') ): ?>
<?php $user = get_field('フィールド名'); ?>
ユーザー:<?php echo $user['user_nicename']; ?>/ <?php echo $user['nickname']; ?>
<?php endif; ?>
/*ユーザー(返り値が「Multi Select」の場合)-----------*/
<?php if( get_field('フィールド名') ): ?>
<?php $user = get_field('フィールド名'); ?>
<?php foreach ( (array)$user as $value ): ?>
ユーザー:<?php echo $user['user_nicename']; ?>/ <?php echo $user['nickname']; ?>
<?php if(next($user)!==FALSE){ echo ", ";} ?>
<?php endforeach; ?>
<?php endif; ?>
/*-----------------------------------
フィールドタイプ:jQuery
-----------------------------------*/
/*Google Map(例:仙台駅 lat:38.260115 ing:140.882309)-----------*/
<?php $location = get_field('フィールド名'); ?>
<?php if( !empty($location) ): ?>
<?php echo $location['address'].'<br>lat:'.$location['lat'].'<br>lng:'.$location['lng']; ?>
<iframe src="http://maps.google.co.jp/maps?q=<?php echo $location['address']; ?>&output=embed" width="600" height="450" frameborder="0" style="border:0" allowfullscreen></iframe>
<?php endif; ?>
/*デイトピッカー-----------*/
<?php if( get_field('フィールド名') ): ?>
<?php the_field('フィールド名'); ?>
<?php endif; ?>
/*カラーピッカー-----------*/
<?php if( get_field('フィールド名') ): ?>
<p style="background-color:<?php the_field('フィールド名'); ?>">カラーピッカー:<?php the_field('フィールド名'); ?></p>
<?php endif; ?>
/*-----------------------------------
フィールドラベルを表示する方法
get_field_object()
-----------------------------------*/
<?php if( get_field('フィールド名') ):
the_field('フィールド名');
$fielddata = get_field_object('フィールド名');
echo esc_html($fielddata['label']);
endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment