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 回答