自定义插件的根目录中有两个文件“ my-plugin.php”和“ test.view.php”。 “ my-plugin.php”的内容为:
/*
Plugin Name: test
Plugin URI: test.com
Description: test
Version: 1.0
Author: test
Author URI: test
License: GPLv2+
Text Domain: conference
*/
class Test{
function __construct() {
add_shortcode('testShortCode' , array( $this, 'shortCode'));
}
function shortCode() {
return include 'test.view.php';
}
}
new Test();
“ test.view.php”是:
<h1>Test</h1>
答案 0 :(得分:2)
处理返回: include 在失败时返回FALSE并引发 警告。成功包含,除非被包含的文件覆盖, 返回 1 。
因此,要摆脱您看到的 1 ,您可以将test.view.php
的内容更改为:
return "<h1>Test</h1>";
...或将shortCode()
函数更改为:
function shortCode() {
include 'test.view.php';
}
答案 1 :(得分:1)
您也可以按照以下步骤进行操作:
function shortCode() {
ob_start();
require_once('test.view.php');
$data = ob_get_contents();
ob_end_clean();
return $data;
}