wordpress 中的 get_the_* 和 the_* 模板标签有什么区别? [英] What is the difference between get_the_* and the_* template tags in wordpress?

查看:30
本文介绍了wordpress 中的 get_the_* 和 the_* 模板标签有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 get_the_*the_* 模板标签感到困惑.我已经多次将这些用于我的主题,但我不清楚何时使用 get_the_* 以及何时使用 the_* .请您解释清楚这两个概念.

I am confuse about get_the_* and the_* template tags. I have used those many times to my theme but i am not clear enough when to use get_the_* and when to use the_* . Would you please explain both concept clearly.

推荐答案

通常,get_the_*the_* 函数之间有两个主要区别.

Typically, there are two key differences between get_the_* and the_* functions.

  1. get_the_* 方法本身不回显任何内容.相反,它们通常以字符串形式返回您感兴趣的值.例如,get_the_time() 不回显,并返回字符串表示当前帖子的发布时间.the_* 方法直接输出相同的值,不需要你回显;the_time() 不返回任何内容,而是直接回显发布时间.

  1. get_the_* methods don't echo anything themselves. Instead, they return the value that you're interested in, normally as a string. For example, get_the_time() echoes nothing, and returns a string representation of the posting time of the current post. the_* methods directly output the same value, without you having to echo it; the_time() returns nothing, but directly echoes the posting time.

the_* 方法通常设计用于循环,因此他们通常不带参数来指定您要询问的帖子;例如,the_title() 不需要 post_id 参数,因此只能作用于循环内的当前"帖子.在循环之外调用它是没有意义的——它会获得哪个帖子的标题?但是,get_the_title() 将帖子 ID 作为参数,因此你可以从任何地方使用它来获取任何帖子的标题,只要你有帖子的 ID.(许多 get_the_ 方法采用可选的 post id 参数,如果在循环中使用它们,为了方便,默认返回当前帖子的值.)

the_* methods are generally designed to be used inside the Loop, so they often don't take a parameter to specify which post you're asking about; for example, the_title() doesn't take a post_id parameter, and can therefore only act on the "current" post inside the Loop. It doesn't make sense to call it outside the loop—which post would it be getting the title for? However, get_the_title() takes a post ID as a parameter, so you can use it from anywhere to get the title of any post, as long as you've got the post's ID. (Many of the get_the_ methods take an optional post id parameter, and default to returning the value for the current post if they're used from in the Loop, for convenience.)

因为 WordPress 已经开发了这么多年,而且东西也逐渐增加,所以这些都不是保证规则,你会在这里和那里找到例外.您应该将此作为一般性建议,并根据需要查看每个特定实例的文档.

Because WordPress has been in development for so many years, and things have gradually been added, these aren't guaranteed rules, and you'll find exceptions here and there. You should take this as general advice and check the documentation for each specific instance as you need it.

这篇关于wordpress 中的 get_the_* 和 the_* 模板标签有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆