【问题标题】:Display Advanced Custom Fields through a functions in WooCommerce通过 WooCommerce 中的功能显示高级自定义字段
【发布时间】:2019-10-10 02:45:47
【问题描述】:

我正在尝试将一组高级自定义字段值添加到 WooCommerce 网站中的单个产品页面。

此字段是网站管理员将检查的复选框,我希望这些值显示在单个产品页面中。

我已经找到了如何在我的子主题的 functions.php 文件中使用此代码显示值:

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

其中“cuidados”是字段的名称。

它有效,但这只显示用逗号分隔的值。

现在我想使用更高级的方式来显示值,例如我在 ACF 文档中找到的用于显示复选框值的示例:

<?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; ?>

我不了解 PHP,但我知道由于语法原因,这不适用于我的 functions.php 文件。

你能帮我弄清楚如何做到这一点吗?

提前致谢。

【问题讨论】:

  • 我知道一开始学习一门编程语言会很困难,但是如果你还没有深入学习 PHP 来转换基本语法,那么如果不为你做所有的工作就很难帮助你.此外,我个人认为如果充分适应您自己的数据结构和变量,示例中的语法将无法正常工作。

标签: php wordpress woocommerce product advanced-custom-fields


【解决方案1】:

试试

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; 
}  

【讨论】:

  • 嗨@chatnoir,感谢您的帮助。那没有用,我认为这是因为我在问题中提到的语法。我想我不能在functions.php中使用&lt;?php标签,所以我需要弄清楚如何以正确的格式添加这个函数作为我提供的第一个代码并且它已经在工作了。有什么想法吗?
【解决方案2】:

get_field('cuidados') 的值是由逗号分隔的字符串,您可以尝试以下方法:

$colors_str = get_field('colors');

if( ! empty($colors_str) ) {

    // Removing the space after the coma (if there is any)
    $colors_str = str_replace(', ', ',', $colors_str);

    // Convert the string as an array
    $colors = explode(',', $colors_str);

    // Html output
    echo '<ul><li>' . implode( '</li><li>', $colors ) . '</li></ul>';
}

它应该可以工作......所以在你的钩子函数中:

add_action( 'woocommerce_product_tabs', 'deco_display_acf_field_under_images', 30 );
function deco_display_acf_field_under_images() {
    global $product;

    $colors_str = get_field('colors', $product->get_id());

    if( ! empty($colors_str) ) {

        // Removing the space after the coma (if there is any
        $colors_str = str_replace(', ', ',', $colors_str);

        // Convert the string as an array
        $colors = explode(',', $colors_str);

        // Html output
        echo '<ul><li>' . implode( '</li><li>', $colors ) . '</li></ul>';
    }
}

代码进入您的活动子主题(或活动主题)的functions.php 文件中。

【讨论】:

  • 嗨,@loictheaztec 感谢您的帮助,我将代码添加到我的functions.php(只是将示例中的字段名称更改为实际字段名称):[链接](snag.gy/eE7OFb.jpg)但在前端我只得到一个空列表标签:[link] (snag.gy/f3Xic4.jpg) 有什么想法吗?
  • @CarlosRodríguez get_field('colors'); 的输出是什么,我的意思是你到底得到了什么? …没有人能真正回答你的问题,因为你应该举一个你得到的输出的例子get_field('colors');
猜你喜欢
  • 2018-08-26
  • 2020-09-29
  • 2016-11-13
  • 2016-07-18
  • 1970-01-01
  • 2020-09-19
  • 1970-01-01
  • 2019-08-20
  • 2018-04-15
相关资源
最近更新 更多