为什么我的短代码会在其他内容之前执行?

Rom*_*man 3 php wordpress

我在页面中有以下文字.正如您所看到的,我的短代码位于底部,但不知何故,当代码运行时,我的短代码的输出将插入页面顶部,而不是跟随其前面的内容.

<img class="alignnone" title="title_enquires" src="http://localhost/barcelona/wp-content/uploads/2011/08/title_enquires.jpg" alt="" width="589" height="77" />
<img class="alignnone" title="contact_map" src="http://localhost/barcelona/wp-content/uploads/2011/08/contact_map.jpg" alt="" width="555" height="222" />

[barcelona_address]
Run Code Online (Sandbox Code Playgroud)

这是我在function.php文件中的短代码注册:

<?php
add_shortcode( 'barcelona_address', 'barcelona_shortcode_handler' );

function barcelona_address_func()
{
    print "<p>sdsdsds</p>";
}

function barcelona_shortcode_handler( $atts, $content=null, $code="" ) 
{
   if (function_exists($code . "_func"))
   {
       call_user_func($code . "_func", $atts);
   }
}
?>
Run Code Online (Sandbox Code Playgroud)

结果是:

<p>sdsdsds</p>
<img class="alignnone" title="title_enquires" src="http://localhost/barcelona/wp-content/uploads/2011/08/title_enquires.jpg" alt="" width="589" height="77" />
<img class="alignnone" title="contact_map" src="http://localhost/barcelona/wp-content/uploads/2011/08/contact_map.jpg" alt="" width="555" height="222" />
Run Code Online (Sandbox Code Playgroud)

Dan*_*man 13

你的短代码处理程序应该返回输出显示代替短代码,而不是输出任何东西.

http://codex.wordpress.org/Shortcode_API


小智 5

您可以替代使用:

ob_start();
...your code...
return ob_get_clean();
Run Code Online (Sandbox Code Playgroud)

并使用而不返回html作为字符串。