【问题标题】:Display ACF custom fields on WooCommerce store home and category listing在 WooCommerce 商店主页和类别列表中显示 ACF 自定义字段
【发布时间】:2019-09-13 18:29:57
【问题描述】:

我正在尝试在我的 WooCommerce 店面中以 html 列表标记显示高级自定义字段。我修改了文件:

content-product_cat.php

从此:

<li <?php wc_product_cat_class( '', $category ); ?>>

到这里:

<li style="background: <?php the_field('my_ACF_field'); ?>" <?php wc_product_cat_class( '', $category ); ?>>

我还在我的 WP 后端添加了 ACF 字段,并将其分配给我的名为“Store”的页面,这是 WooCommerce 店面。我可以在我的后端(商店页面)上填写输入,但它没有显示在店面/类别页面上,只是没有通过。问题出在哪里?

我也尝试将此字段设置为分类,并将其显示在 WooCommerce 的类别设置中,也没有成功。

【问题讨论】:

  • 分类页面不是店铺页面。也许将 acf 字段添加到 WC 产品类别。
  • 同样没有成功

标签: php wordpress woocommerce advanced-custom-fields


【解决方案1】:

我认为您需要使用 ACF the_field 上的第二个参数来获取您所追求的特定对象的值。例如。如果您在产品类别上设置了 ACF 字段,则需要传入您当前正在查看的术语以获得正确的值。

我相信在类别页面上您需要使用类似

<li style="background: <?php the_field('my_ACF_field', $category);?>" <?php wc_product_cat_class( '', $category ); ?>>

此 ACF 文档字段提供了有关向分类术语添加字段的更多详细信息:https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

【讨论】:

  • 谢谢!这项工作对我有用,但是是否可以更改此代码以将 ACF 字段放在“商店”页面而不是 WooCommerce 类别设置中?
  • 尝试传入页面 post_id 作为第二个参数。不过,我不太清楚您在哪里尝试使用商店页面上的 ACF 字段。 ACF the_field 函数的文档可能有帮助吗? advancedcustomfields.com/resources/the_field
  • 好的,我通过将变量 $post_id = 61; 添加到我的 content-product_cat.php 并将 $category 更改为 $post_id 但仍然试图弄清楚如何在 functions.php 文件中做同样的事情跨度>
  • 你想在functions.php中用它做什么?如果您想在变量中使用值,请尝试使用 get_field。同样,来自 ACF 文档:advancedcustomfields.com/resources/get_field
猜你喜欢
  • 2021-05-15
  • 2019-08-22
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-06
  • 1970-01-01
相关资源
最近更新 更多