Advanced Custom Fields - yagisawatakuya/Wiki GitHub Wiki
参考:https://on-ze.com/archives/4961
基本
<?php the_field('text'); ?>
画像の出力
<?php
$mainvisual_attachment_id = get_field('img');
$mainvisual_size = 'full';
$mainvisual_image = wp_get_attachment_image_src( $mainvisual_attachment_id, $mainvisual_size );
$mainvisual = $mainvisual_image[0];
echo $mainvisual;
?>
日付
<?php
$week = array( "日", "月", "火", "水", "木", "金", "土" );
$date = DateTime::createFromFormat('Ymd', get_field('event-date'));
$datetime = new DateTime($date->format('Y-m-d'));
echo $date->format('Y'); //年
echo $date->format('m'); //月
echo $date->format('d'); //日
echo $date->format('D'); //曜日
echo $week[$datetime->format("w")]; //曜日 日本語
?>
Relationship
<?php
$relation_posts = get_field('relation');
if($relation_posts):
?>
<ul>
<?php foreach( $relation_posts as $post ): ?>
<li><a href="<?php echo get_permalink( $post->ID ); ?>"><?php echo get_the_title( $post->ID ); ?></a></li>
<?php endforeach; ?>
</ul>
<?php endif; wp_reset_postdata(); ?>
has_sub_field
<?php
if(get_field("content")): while(has_sub_field("content")):
if(get_row_layout() == "text"):
echo get_sub_field('text');
endif;
endwhile; endif;
?>
meta_query
$args = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'color',
'value' => 'red',
'compare' => 'NOT LIKE',
),
array(
'key' => 'price',
'value' => array( 500, 2000 ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
);
参考:http://www.kerenor.jp/loop-sorted-by-customfield/
指定したget_sub_fieldを一つのみ取出す
<?php
$count = 0;
while(the_flexible_field("content")):
if(get_row_layout() == "text"): $count++; // layout - count
if($count == 1):
$first_txt = get_sub_field("text");
$first_row_txt_html = strip_tags($first_txt); //hmtlタグを取り除く
echo mb_substr($first_row_txt_html, 0, 100, 'UTF-8'), '…'; //文字数100文字に設定
endif;
endif;
endwhile;
?>
get_row_layoutの最初にあるテキストを出力
<?php
$rows = get_field ( 'content' );
$first_row = $rows[0];
$first_row_txt = $first_row['text'];
echo $first_row['text'];
?>
チェックボックスの値を表示
$cfcb = get_field_object('field_name');
$cfcbId = get_post_meta($post->ID,'field_name');
$cfcbId = $cfcbId[0];
if($cfcb) {
foreach($cfcbId as $v) {
echo '<span class="ico-'. $v. '">' . $cfcb['choices'][$v] .'</span>';
}
}
参考:http://wp-dsrt.hateblo.jp/entry/2015/03/29/163355
ACFのwysiwygエディタの機能を制限
add_filter('acf/fields/wysiwyg/toolbars', 'my_toolbars');
function my_toolbars( $toolbars ){
// Uncomment to view format of $toolbars
/*
echo '< pre >';
print_r($toolbars);
echo '< /pre >';
die;
*/
// Add a new toolbar called "Very Simple"
// - this toolbar has only 1 row of buttons
$toolbars['Very Simple'] = array();
$toolbars['Very Simple'][1] = array('bold', 'italic', 'link', 'unlink');
$toolbars['Very Simple (No Link)'] = array();
$toolbars['Very Simple (No Link)'][1] = array('bold', 'italic');
// Edit the "Full" toolbar and remove 'code'
// - delet from array code from http://stackoverflow.com/questions/7225070/php-array-delete-by-value-not-key
if( ($key = array_search('code',$toolbars['Full' ][2])) !== false){
unset( $toolbars['Full'][2][$key]);
}
// return $toolbars - IMPORTANT!
return $toolbars;
}