2011-06-06 125 views
2

我写了一个基于ID显示作者个人资料的短代码。例如[user-profile id =“1”]将显示作者1在user-profile.php中定义的配置文件块。它工作(即使在同一页面上有多个实例)。多个实例破解WordPress短代码

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”]会将其分开 - 页面刚刚停止加载。

我该如何修改这个以允许多个实例?

+1

如果出现什么错误,您会收到什么错误?我怀疑它是由于在函数中定义了一个函数,你有没有试过把'function get_user_profile(){}'移出user_profile函数 – 2011-06-06 18:25:29

+0

,请将你的评论移到回答部分,并将其标记为正确的答案。 – ariefbayu 2011-06-07 04:52:51

+0

@silent谢谢,谢谢,是的,我试过之前,但它说我不能做8小时。 – ryanve 2011-06-10 22:02:28

回答

0

问题解决了!我更新了user-profile.php代码中的代码,以便它是全部PHP并且不使用任何回声。然后,我将短代码功能更改为:

0

试试这个方法:

[user-profile id="1"][/user-profile] [user-profile id="2"][/user-profile]