如何在自定义 wordpress 短代码后删除不需要的号码?

How to remove unwanted number after custom wordpress shortcode?

我在自定义插件的根目录中有两个文件 "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>

我将 [testShortCode] 放在一个页面中,但在打印测试后我看到它后面有一个“1”。

来自documentation

Handling Returns: include returns FALSE on failure and raises a warning. Successful includes, unless overridden by the included file, return 1.

因此,要删除您看到的 1,您可以将 test.view.php 内容更改为:

return "<h1>Test</h1>";

... 或者您将 shortCode() 函数更改为:

function shortCode() {
    include 'test.view.php';
}

您也可以按照以下方式进行:

function shortCode() {
    ob_start();
    require_once('test.view.php');
    $data = ob_get_contents();
    ob_end_clean();
    return $data;
}

参考: