dqmg80654 2013-01-18 13:01
浏览 119
已采纳

高级自定义字段复选框

I am using advanced custom fields Checkbox feature in wordpress.

I have created a <ul> with four <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)".

<?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. 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. I mean that if you Check a Checkbox on Image and text it would print .active or something wise on

<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 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. 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

  • 写回答

1条回答 默认 最新

  • douqu2712 2013-01-18 16:30
    关注

    It's not really clear from the Question what Fields exist.

    In the case of a Checkbox field (check PHP ternary operator):

    // 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):
    acf select multiple

    $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> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 centos7中sudo命令无法使用
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头