显示 WooCommerce 自定义分类法 ACF 字段
Posted
技术标签:
【中文标题】显示 WooCommerce 自定义分类法 ACF 字段【英文标题】:Display WooCommerce Custom Taxonomy ACF Field 【发布时间】:2021-10-06 16:29:45 【问题描述】:我为 woocommerce 产品“coffee_type”创建了自定义分类。 我为此分类创建了一个 ACF 图像字段“coffee_type_image”。
我想在产品页面上显示带有链接而不是分类名称的图像。 我在这里阅读了大多数关于在分类上显示 acf 图像的文章,但每一篇文章都是关于使用存档分类页面而不是产品页面。 我正在编辑 single-product.php(确切地说是 content-single-prodct.php)。
这是我当前的循环
<?php $wcatTerms = get_terms('coffee_type', array('hide_empty' => 0, 'parent' => 0));
foreach ($wcatTerms as $wcatTerm):
?>
<ul>
<li>
<a href="<?php echo get_term_link($wcatTerm->slug, $wcatTerm->taxonomy); ?>"><?php echo $wcatTerm->name; ?></a>
<?php $taxonomy = $queried_object->taxonomy;?>
</li>
</ul>
<?php
endforeach;
?>
这是#wcatTerm 的var_dump
object(WP_Term)[3704]
public 'term_id' => int 33
public 'name' => string 'coffee and chicory' (length=18)
public 'slug' => string 'coffee-and-chicory' (length=18)
public 'term_group' => int 0
public 'term_taxonomy_id' => int 33
public 'taxonomy' => string 'coffee_type' (length=11)
public 'description' => string '' (length=0)
public 'parent' => int 0
public 'count' => int 2
public 'filter' => string 'raw' (length=3)
这是get_queried_object()的var_dump;
object(WP_Post)[3316]
public 'ID' => int 671
public 'post_author' => string '1' (length=1)
public 'post_date' => string '2021-07-27 14:04:09' (length=19)
public 'post_date_gmt' => string '2021-07-27 14:04:09' (length=19)
public 'post_content' => string '<!--(figmeta)eyJmaWxlS2V5IjoiOWxOQjFWT01TaWFzVmx6cmxTUTl3YiIsInBhc3RlSUQiOjE2MjEzNjQ4LCJkYXRhVHlwZSI6InNjZW5lIn0K(/figmeta)--><!--(figma)ZmlnLWtpd2kEAAAARCMAALV7f5wsS1VfVc/M/rh7730/eTyeiIiIiKjvF+89EJGenp7dvjsz3a+7Z/beJzL0zvTuzruzM8P0zN67T0REQgxBRFQgiAQJUUSjqPgrQUVi1CSKvxERFRGNMYlJzC9jjMn3W9W/5u59fvzH+/ncqVOnTp06deqcU6eqet8m23GSRIdxeDqLhbjlkut0+kFo+qHAv47bsPvWjtnZtgNUZTew/VLdUNR2pwG4EjjbHbMFqBqEV1o2gJoC+oFNXmuKVnHuB7uO1/ftlmuy53rHDZ3mlX6w43ZbjX7X2/bNBvtvpGC/4XZY38zqvt307WAHqHOBZXfsPtDeTv/Rru1fAXKrjPRtr0Xk+Yb'... (length=52896)
public 'post_title' => string 'Product Name 1' (length=14)
public 'post_excerpt' => string '<!--(figmeta)eyJmaWxlS2V5IjoiOWxOQjFWT01TaWFzVmx6cmxTUTl3YiIsInBhc3RlSUQiOjE2MjEzNjQ4LCJkYXRhVHlwZSI6InNjZW5lIn0K(/figmeta)--><!--(figma)ZmlnLWtpd2kEAAAARCMAALV7f5wsS1VfVc/M/rh7730/eTyeiIiIiKjvF+89EJGenp7dvjsz3a+7Z/beJzL0zvTuzruzM8P0zN67T0REQgxBRFQgiAQJUUSjqPgrQUVi1CSKvxERFRGNMYlJzC9jjMn3W9W/5u59fvzH+/ncqVOnTp06deqcU6eqet8m23GSRIdxeDqLhbjlkut0+kFo+qHAv47bsPvWjtnZtgNUZTew/VLdUNR2pwG4EjjbHbMFqBqEV1o2gJoC+oFNXmuKVnHuB7uO1/ftlmuy53rHDZ3mlX6w43ZbjX7X2/bNBvtvpGC/4XZY38zqvt307WAHqHOBZXfsPtDeTv/Rru1fAXKrjPRtr0Xk+Yb'... (length=22771)
public 'post_status' => string 'publish' (length=7)
public 'comment_status' => string 'open' (length=4)
public 'ping_status' => string 'closed' (length=6)
public 'post_password' => string '' (length=0)
public 'post_name' => string 'product-name-1' (length=14)
public 'to_ping' => string '' (length=0)
public 'pinged' => string '' (length=0)
public 'post_modified' => string '2021-07-31 11:57:41' (length=19)
public 'post_modified_gmt' => string '2021-07-31 11:57:41' (length=19)
public 'post_content_filtered' => string '' (length=0)
public 'post_parent' => int 0
public 'guid' => string 'http://localhost/orc/?post_type=product&p=671' (length=50)
public 'menu_order' => int 0
public 'post_type' => string 'product' (length=7)
public 'post_mime_type' => string '' (length=0)
public 'comment_count' => string '3' (length=1)
public 'filter' => string 'raw' (length=3)
如何在 ACF 字段中显示“coffee_type_image”以及指向存档页面的链接?我不知道如何将 ACF 传递给此分类法以使用 get_field() 显示图像;
我尝试过类似的东西
<?php $term = get_field('coffee_type_image', $wcatTerm->term_id);?>
和
<?php $term = get_field('coffee_type_image', $wcatTerm->term_id, $wcatTerm->taxonomy);?>
但它们总是返回 null 谢谢
【问题讨论】:
【参考方案1】:我刚刚找到了我的问题的答案。如果其他人需要,我会发布。
<?php $term = get_field('coffee_type_image', 'coffee_type_' . $wcatTerm->term_id);?>
<a href="<?php echo get_term_link($wcatTerm->slug, $wcatTerm->taxonomy); ?>"><img src="<?php echo $term; ?>" class="img-responsive"></a>
【讨论】:
以上是关于显示 WooCommerce 自定义分类法 ACF 字段的主要内容,如果未能解决你的问题,请参考以下文章
WooCommerce ACF 在购物车和结帐页面上显示自定义元数据
在 WooCommerce 订单管理页面 (ACF) 中输出产品自定义字段