drtwqc3744 2019-05-23 13:46
浏览 41

通过WooCommerce中的功能显示高级自定义字段

I'm trying to add an array of Advanced Custom Fields values to the single product page in a WooCommerce website.

This fields are checkboxes the website admin will check and I want the values to show in the single product page.

I've already found how to show the values using this code on my child theme's functions.php file:

add_action( 'woocommerce_product_tabs', 'deco_display_acf_field_under_images', 30 );
    function deco_display_acf_field_under_images() {
       echo the_field('cuidados');
    }

Where “cuidados” is the name of the field.

It works, but this only displays the values separated by commas.

Now I want to use a more advanced way of displaying the values like this example I've found on the ACF documentation for displaying checkboxes values:

<?php

// vars 
$colors = get_field('colors');


// check
if( $colors ): ?>
    <ul>
    <?php foreach( $colors as $color ): ?>
        <li><?php echo $color; ?></li>
    <?php endforeach; ?>
    </ul>
<?php endif; ?>

I don't know PHP but I know this won't work on my functions.php file because of the syntax.

Can you help me figure out how to achieve this?

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dqwp81696 2019-05-23 14:05
    关注

    Try

    add_action('woocommerce_product_tabs', 'deco_display_acf_field_under_images', 30);
    
    function deco_display_acf_field_under_images() {
    // fields MUST be an array in the format array(a,b,c) (or [a,b,c])
    $fields = the_field('cuidados');
    if (is_string($fields)) $fields = explode(",", $fields);
    
    // check
    if ($fields): ?>
        <ul>
            <?php foreach ($fields as $field): ?>
                <li><?php echo $field; ?></li>
            <?php endforeach; ?>
        </ul>
    <?php endif; 
    }  
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题