我编写了一个短代码,它根据 id 显示作者个人资料。例如 [user-profile id="1"] 将显示在user-profile.php中为作者 1 定义的配置文件块。它有效(即使在同一页面上有多个实例)。
function user_profile( $atts, $content = null ) {
extract(shortcode_atts(array('id' => ''), $atts));
include 'user-profile.php';
}
...除了短代码输出显示在其他条目内容之前,无论其在代码中的位置如何。为了解决这个问题,我添加了这个修复:
function user_profile( $atts, $content = null ) {
extract(shortcode_atts(array('id' => ''), $atts));
function get_user_profile() { include 'user-profile.php'; }
ob_start();
get_user_profile();
$output_string = ob_get_contents();
ob_end_clean();
return $output_string;
}
...这可以解决定位问题,但破坏了短代码的多个实例。[user-profile id="1"] 有效,但 [user-profile id="1"] [user-profile id="2"] 破坏了它——页面此时停止加载。
如何修改它以允许多个实例?