0

Wordpress ページでショートコードを使用しています。クエリ文字列を使用して、いくつかのデータをショートコードと比較します。クエリ文字列は次のようになります: ?results&answer1=1&answer2=1

Wordpress の使用例:

[answer question=1 image=1]
Lorem Ipsum Doler Sit Amet
[answer question=2 image=1]

ただし、結果は次のとおりです。

Image Image
Lorem Ipsum Doler Sit Amet

私が達成したいこと:

Image
Lorem Ipsum Dolder Sit Amet
Image

私が書いたコード:

function survey_shortcode($atts)
{

    $return = null;
    // Check if ?results is set in the URL
    if (isset($_GET['results'])) {

        // Loop all url values
        foreach ($_GET as $key => $value) {

            // Check if value url matches 'answer'
            if (trim($key, ' 0123456789') == 'answer') {
                //Split data by "-" to array
                $data = explode("-", $value);

                //Check if question matches and has variables set (image)
                if ($atts['question'] == $data[0] && $atts['image'] == true) {

                    // If value is true or 1 -> show image true, otherwise show image false
                    if ($data[0] == "true" || $data[0] == "1") {
                        $return .= '<img src="/wp-content/themes/markteffectief/images/navbg.png">';
                    } else if ($data[0] == "false" || $data[0] != "0") {
                        $return .= '<img src="/wp-content/themes/markteffectief/images/navbg.png">';
                    }
                // If no image variable is set -> view data
                } else if ($atts['question'] == $data[0]) {
                    $return = $data[0];
                }
            }
        }
    } else {
        $return = null;
    }

    return $return;
}

add_shortcode('answer', 'survey_shortcode');

$_GET を介してループを実行し、すべてを $return .= some-image に追加すると思います。すぐに返してほしいので、正しい位置(ショートコードを設定した位置)に表示されます。どうすればこれを解決できますか?

4

1 に答える 1

0

おそらく、コンテンツを解析してショートコードを自分のものに置き換える the_content フックを試す必要があります

function my_the_content_filter($content) {
 return str_replace("[shortcode]", "your code", $content);
}
add_filter( 'the_content', 'my_the_content_filter' );
于 2013-08-06T10:18:42.980 に答える