【问题标题】:WordPress get_terms returning empty arrayWordPress get_terms 返回空数组
【发布时间】:2011-11-28 22:44:47
【问题描述】:

我遇到了 get_terms() 函数的问题。

如果我使用:

<?php
$terms = get_terms( 'location' );
print_r($terms);
?>

我得到一个数组:

Array ( [0] => stdClass Object ( [term_id] => 8 [name] => Aberdare [slug] => aberdare [term_group] => 0 [term_taxonomy_id] => 8 [taxonomy] => location [description] => [parent] => 7 [count] => 1 ) [1] => stdClass Object ( [term_id] => 10 [name] => Aberdeen [slug] => aberdeen [term_group] => 0 [term_taxonomy_id] => 10 [taxonomy] => location [description] => [parent] => 9 [count] => 14 ) [2] => stdClass Object ( [term_id] => 17 [name] => Aberdeenshire [slug] => aberdeenshire [term_group] => 0 [term_taxonomy_id] => 17 [taxonomy] => location [description] => [parent] => 9 [count] => 41 ) [3] => stdClass Object ( [term_id] => 1203 [name] => Aberdour [slug] => aberdour [term_group] => 0 [term_taxonomy_id] => 1203 [taxonomy] => location [description] => [parent] => 446 [count] => 1 );

但是,如果我尝试获取任意数量 child_of 数组的父项的条款,则返回为空。

<?php
$terms = get_terms( 'location', array('parent' => 9) );
print_r($terms);
?>

我明白了:

Array ( )

有人知道我做错了什么吗?

谢谢 皮特

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    好的,我解决了这个问题。

    真的有点奇怪。我所有的术语都是通过使用 wp_insert_term 函数的脚本添加的。当我进入仪表板中的 edit-tags.php?taxonomy=location 页面时,只能看到右侧列表中的前三个术语,但我可以在左侧的术语云中看到其他术语。

    然后我进入其中一个术语并保存它,它们都出现在右侧的列表中,也出现在我的数组中。

    我猜他们一定是术语列表上的某种缓存,它需要我通过仪表板保存一个以便查看层次结构。

    我必须承认一个奇怪的人。

    皮特

    【讨论】:

    • 谢谢,我可能会浪费很多时间,如果你在 8 年前没有分享这个:)
    【解决方案2】:

    层次结构被缓存,并且在 wp_insert_term 之后不会自动失效。你可以打电话

    delete_option("{$taxonomy}_children");

    手动强制在下一次访问层次结构时重新生成。

    【讨论】:

      猜你喜欢
      • 2015-09-17
      • 2018-05-31
      • 2018-09-21
      • 2017-06-21
      • 1970-01-01
      • 2018-12-11
      • 1970-01-01
      • 2017-06-20
      • 1970-01-01
      相关资源
      最近更新 更多