【发布时间】:2016-12-30 20:13:04
【问题描述】:
我有一个函数列表,它们运行一个相当深的例程来确定从哪个 post_id 获取其内容并将其输出到站点的前端。
当这个函数返回它的内容时,我希望它被包装在一个 html 包装器中。我希望这个 html 包装器仅在函数有要返回的输出时才加载。
例如,我有以下...
public static function output_*() {
// my routines that check for content to output precede here
// if there IS content to output the output will end in echo $output;
// if there is NO content to output the output will end in return;
}
在完整的解释中,我有以下...
如果其中一个函数返回一个输出,我希望它被包装在一个 html 包装器中,所以理论上我想要完成这样的事情......
public static function begin_header_wrapper() {
// This only returns true if an output function below returns content,
// which for me is other than an empty return;
include(self::$begin_header_wrapper);
}
public static function output_above_header() {
// my routines that check for content to output precede here
// if there is content to return it will end in the following statement
// otherwise it will end in return;
include($begin_markup); // This is the BEGIN html wrapper for this specifc output
// It is, so let's get this option's post id number, extract its content,
// run any needed filters and output our user's selected content
$selected_content = get_post($this_option);
$extracted_content = kc_raw_content($selected_content);
$content = kc_do_shortcode($extracted_content);
echo $content;
include($end_markup); // This is the END html wrapper for this specifc output
}
public static function output_header() {
// the same routine as above but for the header output
}
public static function output_below_header() {
// the same routine as above but for the below header output
}
public static function end_header_wrapper() {
// This only returns true if an output function above returns content,
// which for me is other than an empty return;
include(self::$end_header_wrapper);
}
我现在知道,我不想提前确定两次(一次在开始,一次在结束)是否有一个输出函数有输出,什么时候应该有办法做到这一点一次检查,但我想从这个兔子洞开始,找出确定我的函数是否返回某些东西的最佳方法。
或者如果有更好的方法来解决这个问题,请全力以赴,大声笑告诉我。
我在网上看了这篇文章和其他文章 @Find out if function has any output with php
所以最后,我只是想知道是否有更好的方法来解决这个问题,以及您认为检查我的函数是否有要返回的输出以便我可以运行我的 html 包装器的最佳方法那些条件?
ob_get_length 会是最好的方法吗?当我查看 ob 目的时,这似乎是最好的,也是最简单的,但想得到一些建议和反馈。或者我可以检查我的变量$content 是否返回?谢谢。真的很感激!
【问题讨论】:
-
在我写这篇文章时,我已经可以看到,运行 check less 的最佳方法可能是将我的输出函数合并到一个 output_header 函数中。然后我可以检查一次,针对那个函数而不是针对 3 个不同的函数。但我仍然想知道检查函数是否返回和输出的最佳方法。 - 谢谢!欣赏它。
-
链接的问答可能是您想要做的。只需捕获函数的输出并检查它是否为空。
-
谢谢。 2 件事。我想知道是否可以检查我的变量 $content 是否返回,因为如果不是,那么显然没有要输出的内容,所以我基于此运行我的 html 包装器。那可能吗?或者按照您所说的,可以在类中的函数之外打开输出缓冲吗?这样我就可以在我的三个标题输出之前将其打开并在之后将其关闭,而不是在每个函数中打开和关闭它。我环顾四周,没有看到任何说明或表明这是否可以或具体可以在课堂上完成的内容? @Rizier123
-
不,您不能只检查返回值,因为如果有输出,您将无法再捕获它。是的,您可以在方法内的类中启动输出缓冲,调用您的函数,然后检查您是否捕获了某些内容。
-
@Rizier123 太棒了!!!谢谢!我明白那个。我不知道 $f = function_name 和 $p = params。这很有帮助,哈哈。现在是有道理的。谢谢!如果您认为将您的评论作为答案是有效的,我可以将其标记为已回答。谢谢!
标签: php wordpress wordpress-theming output