自定义wordpress简码后如何删除不需要的数字?

时间:2018-10-26 13:32:42

标签: wordpress 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”。 enter image description here

2 个答案:

答案 0 :(得分:2)

来自documentation

  

处理返回: 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;
}

参考:https://stackoverflow.com/a/33805702/1082008

相关问题