what is the difference between get_terms and get_the_terms in WordPress ?

依然范特西╮ 提交于 2019-12-10 03:32:08

问题


I need your help.

My question is what is the difference between get_terms and get_the_terms in WordPress ?

I know that get_the_terms for example give me set of categories that a post attached to them, but really I can't understand what get_terms do and how it different from get_the_terms ?

thank you all.


回答1:


In the WordPress Codex you can find:

For get_the_terms: "Retrieve the terms of the taxonomy that are attached to the post." http://codex.wordpress.org/Function_Reference/get_the_terms

For get_terms: "Retrieve the terms in a taxonomy or list of taxonomies." http://codex.wordpress.org/Function_Reference/get_terms

So, as you said, get_the_terms() will get the terms (e.g. categories) attached to a post, whereas get_terms() will retrieve the terms in a taxonomy (e.g. categories in the category taxonomy). For example, get_terms( 'category' ) will return all categories that you have added to your WordPress site.




回答2:


In a more practical understanding, you can use get_terms outside of the loop, whilst you can only use get_the_terms inside of the loop.




回答3:


In get_the_terms you can fetch terms by a particular post_id, but in get_terms you can get set of values for set of post_id (ie, post) .



来源:https://stackoverflow.com/questions/24925297/what-is-the-difference-between-get-terms-and-get-the-terms-in-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!