2016/06/01
カスタムフィールド カスタム投稿
備忘録です。
参考サイト
Advanced Custom Fieldsによるカスタムフィールドの出力 [チェックボックス・ラジオボタン編]
カスタムフィールドでtestのチェック項目を作成します。
フィールド名は「test」にします。
選択肢は「red : 赤」「blue : 青」にします。
<p>TEST: <?php the_field('test'); ?></p>
<?php
$colors = get_field('test');
if ($tests): 
?>
<ul>
    <?php
    foreach ($tests as $test) : ?>
        <li><?php echo $test; ?></li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>
get_field_objectを使用
<?php
$field = get_field_object('test');
$tests = $field['value'];
if( $tests ): 
?>
 
<ul>
    <?php foreach( $tests as $test ): ?>
    <li><?php echo $field['choices'][ $test ]; ?></li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>
get_fieldを使用
<?php
$tests = get_field('test');
if( $tests ): 
?>
 
<ul>
    <?php foreach( $tests as $test ): ?>
    <li><span class="test-<?php echo $test['value']; ?>"><?php echo $test['label']; ?></span></li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>
$checked = get_field('field_name');
if($checked){ //もし項目を選択したら
     //選択した時の処理
}else[
     //選択していない時の処理
}
 
//逆の場合は...
 
if( !$checked ){ //もし項目を選択していなかったら
    //選択していない時の処理
}else{
    //選択した時の処理
}
<?php
$tests = get_field('test');
if( $tests && in_array('red', $tests ) ) { //もし赤が選択されていたら
    echo '<img src="画像URL" />'; //赤のアイコンを表示する
}
?>
<?php
$test = get_field('test');
if ($test == 'red'){
    echo '赤色を選択しました';
} elseif ($test == 'blue'){
    echo '青色を選択しました';
}elseif ($test == 'yellow'){
    echo '黄色を選択しました';
}elseif ($test == false){
    echo '選択していません';
}else{
    echo '他の色'; //もし上記以外の色があった場合など
}
?>
いろいろと応用できそうです。
ゼヒトモ内でのプロフィール: ROCKSTREAM, ゼヒトモのホームページ作成・制作サービス, 仕事をお願いしたい依頼者と様々な「プロ」をつなぐサービス
2025/01/31
JQuery
2025/01/01
神社
御朱印
相模原
2024/10/27
ブラウザ
カスタム投稿
Wordpress
2024/08/20
神社
御朱印
2024/07/06
神社
御朱印
