当我试图在elementor中包含我的自定义wordpress插件快捷代码时,我遇到了一个奇怪的问题。每当我使用外部php文件时,都没有输出。
此代码运行良好:
// Shortcode Output function
function vergleichsplugin_output_frontend()
{
ob_start();
echo '<div class="vergleichsplugin"></div>';
return ob_get_clean();
}
/* Shortcodes */
add_shortcode('vergleichsplugin','vergleichsplugin_output_frontend');
但这根本不会产生任何输出(文件路径是正确的(:
// Shortcode Output function
function vergleichsplugin_output_frontend()
{
ob_start();
$html = require_once(ABSPATH.'/wp-content/plugins/vergleichsplugin/views/frontend/frontend.php');
$html = $html.ob_get_clean();
return $html;
}
/* Shortcodes */
add_shortcode('vergleichsplugin','vergleichsplugin_output_frontend');
frontend.php的内容相同:
echo '<p>Output</p>';
你试过这样的东西吗?
function vergleichsplugin_output_frontend() {
ob_start();
include(ABSPATH.'/wp-content/plugins/vergleichsplugin/views/frontend/frontend.php');
return ob_get_clean();
}
add_shortcode('vergleichsplugin','vergleichsplugin_output_frontend');
为什么你的解决方案不起作用?
为了将文件的内容保存到具有require_once
或include
的变量中,您需要返回该文件中的所有html。类似于:
// frontend.php
<?php
$html = "<h1>Some sample html</h1>";
return $html;
?>
同样,本节无效:
$html = $html.ob_get_clean();
return $html;
应为:
$html = ob_get_clean();
return $html;
由于使用缓冲区,您不需要将require保存到变量中,因为html将由ob_get_clean
返回;