2

我编写了一个短代码,它根据 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"] 破坏了它——页面此时停止加载。

如何修改它以允许多个实例?

4

2 回答 2

0

试试这个方法:

[user-profile id="1"][/user-profile] [user-profile id="2"][/user-profile]
于 2011-06-07T04:51:48.970 回答
0

问题解决了!我更新了user-profile.php代码中的代码,使其全部是 PHP 并且没有使用任何回声。然后我将简码函数更改为:

function user_profile( $atts, $content = null ) {
    global $post;
    extract(shortcode_atts(array('id' => ''), $atts));
    include 'user-profile.php';
    return $user_hcard;
}
于 2011-06-10T22:00:51.987 回答