简体   繁体   English

高级自定义字段复选框

[英]advanced custom fields Checkbox

I am using advanced custom fields Checkbox feature in wordpress. 我在wordpress中使用高级自定义字段复选框功能。

I have created a <ul> with four <li> 我用四个<li>创建了一个<ul> <li>

<ul class="post-icons">
  <li class="microphone" title="Alt text"></li>
  <li class="video" title="Alt text"></li>
  <li class="text" title="Alt text"></li>
  <li class="image" title="Alt text"></li>
</ul>

And i have created four Checkboxes in advanced custom fields. 我已经在高级自定义字段中创建了四个复选框。 If I use this PHP in the class="(here)" . 如果我在class="(here)"使用此PHP。

<?php echo implode(' ', get_field('field_name')); ?>

It will print the value of the Checkboxes on to the class"" but it will print all four Checkboxes values that i have created. 它将把Checkboxes的值打印到类“”上,但是它将打印我创建的所有四个Checkboxes值。 I only would like to print the right one on the right place. 我只想在正确的位置打印正确的一个。

The best way would to use .active class but advanced custom fields doesn't allow multiple Checkboxes with the same value. 最好的方法是使用.active类,但高级自定义字段不允许使用多个具有相同值的Checkbox。 I mean that if you Check a Checkbox on Image and text it would print .active or something wise on 我的意思是,如果您选中“ 图像文本上的复选框”,它将在.active上打印

<li class="image" title="Alt text"></li> and 
<li class="text" title="Alt text"></li> 

Like = <li class="image active" title="Alt text"></li> and <li class="text active" title="Alt text"></li>

I have four Checkboxes 我有四个复选框

  • Video 视频
  • Text 文本
  • Sound 声音
  • Image 图片

And four <li> 还有四个<li>

 - <li class="video"></li>
 - <li class="text"></li>
 - <li class="microphone"></li>
 - <li class="image"></li>

microphone = Sound 麦克风=声音

I would like to print .active on the right <li> class"" item. 我想在正确的<li> class""项目上打印.active When the corresponding Checkbox is checked 选中相应的复选框时

I will link to the Documentation for advanced custom fields Checkbox I didn't understand a lot of how to create something what I would need for my project. 我将链接到“高级自定义字段的文档”复选框,我不太了解如何创建项目所需的内容。

Documentation 文档

It's not really clear from the Question what Fields exist. 从“问题”中并不清楚是什么字段存在。

In the case of a Checkbox field (check PHP ternary operator ): 如果是Checkbox字段(请检查PHP三元运算符 ):

// If checked, the string is 'active', otherwise empty
$image = ( get_field( 'image_field' ) ) ? 'active' : '';
$video = ( get_field( 'video_field' ) ) ? 'active' : ''; 

<li class="image <?php echo $image; ?>" title="Alt text"></li> 
<li class="video <?php echo $video; ?>" title="Alt text"></li> 

But, better yet is a Select field allowing multiple selections (check PHP in_array ): 但是,更好的是,一个Select字段允许多个选择(请检查PHP in_array ):
acf选择多个

$get_field = get_field( 'field_name' ); 
$image = in_array( 'image', $get_field) ? 'active' : '';
$video = in_array( 'video', $get_field) ? 'active' : '';

<li class="image <?php echo $image; ?>" title="Alt text"></li> 
<li class="video <?php echo $video; ?>" title="Alt text"></li> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM