2

idに基づいて作成者プロファイルを表示するショートコードを作成しました。たとえば、[user-profile id = "1"]は、作成者1のuser-profile.phpで定義されたプロファイルブロックを表示します。これは機能しました(同じページに複数のインスタンスがある場合でも)。

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.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 に答える
0

この方法を試してください:

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