【问题标题】:Get custom field from Wordpress term从 Wordpress 术语中获取自定义字段
【发布时间】:2021-07-10 06:17:43
【问题描述】:

我在自定义分类“product_category”上做了一个简单的循环:

$args = array('hide_empty' => false, 'orderby' => 'term_group', 'parent' => false);
$terms = get_terms('product_categorie', $args);

foreach ($terms as $term) {
    echo "<a href=''>".$term->name."</a>";
}

我在分类中添加了一个自定义字段“webshop_url”。我尝试了多种解决方案来在我的循环中打印该自定义字段,但没有运气。

我已经在我的 foreach 循环中尝试了这些方法:

echo get_field('webshop_url', 'product_categorie', $term->term_id);

echo get_field('webshop_url', $term->term_id);

它不打印任何东西。

我知道可以在分类页面本身上使用get_queried_object()。但这在那个循环中也不起作用。

【问题讨论】:

标签: php wordpress loops taxonomy


【解决方案1】:

你可以这样试试:

ACF Document

$args = array('hide_empty' => false, 'orderby' => 'term_group', 'parent' => false);
$terms = get_terms('product_categorie', $args);

foreach( $terms as $term ) {
    $webshop_url = get_field('webshop_url', 'product_categorie'.'_'.$term->term_id);
    var_dump($webshop_url);
}

【讨论】:

  • 不,仍然没有结果。这有效: echo get_field('webshop_url', $term);
  • @Dennis 这对我有用。我也在很多项目中使用过。谢谢
【解决方案2】:

尝试传递术语 OBJECT

echo get_field('webshop_url', $term);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多