LoginSignup
1
2

More than 3 years have passed since last update.

【WordPress/ACF】ACFを使って作ったカスタムフィールドに入力が無ければ非表示にする。

Posted at

なにこれ?

ACFを使ってカスタムフィールドを作った時、クライアントが入力しない場合があるならばこうやって書く。
そうすれば、値の入力が無い欄は非表示にできる。
その他にも出し分けしたい時に使う。

コード

<?php if(get_field('フィールド名')): //フィールド名の入力がある場合 ?>
<!-- HTMLをここに書く -->
<?php else: //フィールド名の入力が無い場合 ?>
<!-- HTMLをここに書く -->
<?php endif; //フィールド名についてのループ終わり ?>
例|「name_romaji」の入力がある場合とない場合
<?php if(get_field('name_romaji')): //「name_romaji」の入力がある場合 ?>
 <div class="name-inner">
  <div class="name jp"><?php the_field('name'); ?></div>
  <small class="name en"><?php the_field('name_romaji'); ?></small>
 </div>
<?php else: //「name_romaji」の入力が無い場合 ?>
 <div class="name-inner">
  <div class="name jp"><?php the_field('name'); ?></div>
 </div>
<?php endif; ?>

※繰り返しフィールドの中で使用する場合は一番最初の「get_field」を「get_sub_field」にする

リンク

参考サイト

【Advanced Custom Fields】設定と出力、条件分岐、値を入れない時には非表示にしたい | 知りたいねっと

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2