使WP函数返回而不是立即打印 [英] Getting WP Functions to Return rather than Printing Immediately
问题描述
我正在尝试在由各种功能(例如 the_title()
, the_excerpt()
, the_permalink()
等.我想做类似下面的事情.不幸的是,这些函数中的大多数会立即打印其结果,而不是返回它们.此外,我检查了这些参数的可用参数,但没有找到任何强制返回的选项.
I'm trying to build an array within "The Loop" of values provided from various functions like the_title()
, the_excerpt()
, the_permalink()
and others. I'd like to do something similar to what follows. The unfortunate thing is that most of these functions immediately print their results rather than returning them. Furthermore, I have checked the available parameters for these, and haven't found any option to force a return.
if (have_posts()) {
while (have_posts()) {
the_post();
$items[] = array(
"id" => get_the_id(), "the_title" => the_title(),
"the_excerpt" => the_excerpt(), "the_permalink" => the_permalink()
);
}
}
推荐答案
具有可猜测名称的替代函数
显然,有两种不同的方法可以解决此问题.如前所述,某些功能将带有一个替代功能,该功能仅在"get_"之前加注.到开始.例如, the_title()
打印标题,而 get_the_title()
返回标题.
Alternative Functions with Guessable Names
There are apparently a couple different ways to tackle this issue. As noted earlier, some functions will come with an alternative function that merely prepends "get_" to the beginning. For instance the_title()
prints the title, whereas get_the_title()
returns the title.
其他功能不遵循这种做法.例如, the_permalink()
没有替代方法,称为 get_the_permalink()
.相反,它的替代方法只是 get_permalink()
.这可能会造成混淆,因此我建议您在模板标签页面上进行查找.
Other functions don't follow this practice. For instance, the_permalink()
has no alternative called get_the_permalink()
. Instead, its alternative is simply get_permalink()
. This can be confusing, so I reccomend you perform look-ups on the Template Tags page.
此外,某些功能将包含一个参数,该参数使您可以更改正常行为.例如,如果您不想使用 get_the_title()
,则可以简单地使用以下代码:
Additionally, some functions will contain a parameter which allows you to alter the normal behavior. For instance, if you don't wish to use get_the_title()
, you could simply use the following:
<?php $title = the_title('echo=0'); ?>
这会将布尔值设置为false,这意味着将返回该值,而不是回显该值.
This sets the boolean value to false, meaning the value will be returned rather than echo'd.
这篇关于使WP函数返回而不是立即打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!