Possible Duplicate:
problem with short code
Shortcodes are broken.
对于那些在家踢球的人,这里有一个简单的步骤来复制我的问题。
打开新的Wordpress安装(3.4.2)。
2) 进入二十一/功能。php并添加以下内容:
function test() {
echo \'-TEST-\';
}
add_shortcode(\'testshortcode\', \'test\');
3)编辑“hello world”帖子,说:
Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!
[testshortcode]
foobar
4)保存并查看,我的结果如下所示:
-TEST-
Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!
foobar
当它看起来像这样时:
Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!
-TEST-
foobar
What on earth is going on here?
最合适的回答,由SO网友:kovshenin 整理而成
短代码回调必须返回,而不是输出。因此,请使用以下方法:
function test() {
return \'-TEST-\';
}
add_shortcode( \'testshortcode\', \'test\' );
更多信息:
http://codex.wordpress.org/Shortcode_API如果必须使用echo,也可以这样做(如果有很多标记,则很有用&处理字符串很困难)-
function test() {
ob_start();
echo \'-TEST-\';
return ob_get_contents();
}
add_shortcode( \'testshortcode\', \'test\' );