短代码始终显示在 WordPress 的顶部(发布之前)

问题描述 投票:0回答:2

我是 WP 插件开发新手,发现我的短代码显示在页面顶部,但我想将其包含在帖子中的任何位置(而不是顶部)。

我的代码:

function getLotoSecond() {
    $html = file_get_contents_curl('URL__ADDRESS');

    // List
    $start = stripos($html, '<ul id="results-2" class="results-items">');
    $end = stripos($html, '</ul>', $offset = $start);
    $length = $end - $start;

    $list = substr($html, $start, $length);
    //--

    // Regex
    preg_match_all('(<label for="LotoPart_C2_([1-6]|Additional)">([0-9]|[1-9][0-9])<\/label>)', $list, $matches);
    $items = $matches[2];
    //--

    // Output
    echo '<ul>';
    for($i = 0; $i < 7; $i++) {
        if($i == 6) {
            echo '<li style="width:30px;height:30px;border-radius:5px;background-color:#F4A024;color:white;font-size:20px;text-align:center;line-height:30px;display:inline-block;margin:0px 5px 5px 0px;">' . $items[$i] . '</li>';
        } else {
            echo '<li style="width:30px;height:30px;border-radius:5px;background-color:#294A70;color:white;font-size:20px;text-align:center;line-height:30px;display:inline-block;margin:0px 5px 5px 0px;">' . $items[$i] . '</li>';
        }
    }
    echo '</ul>';
    //--
}

然后:

function main($atts) {
    if($atts["type"] == "loto-prvy") {
        getLotoFirst();
    } else if($atts["type"] == "loto-druhy") {
        getLotoSecond();
    }
}
    
add_shortcode('tipo', 'main');

这里应该是什么问题?

php wordpress plugins shortcode wordpress-shortcode
2个回答
5
投票

短代码应仅返回内容,不得

print
echo
任何内容。如果您有一个函数
echo/print
一些数据,那么您需要使用ob_startob_get_clean

以下是如何在短代码中使用

ob
函数

function main( $atts ) {
    ob_start();
    
    if ( $atts['type'] == 'loto-prvy' ) {
        getLotoFirst();
    } elseif ( $atts['type'] == 'loto-druhy' ) {
        getLotoSecond();
    }

    $content = ob_get_clean(); // store buffered output content.

    return $content; // Return the content.
}
add_shortcode( 'tipo', 'main' );

0
投票

我已经通过使用 fnc_slidershowing 函数的返回值解决了这个错误问题。当我们使用 echo 时,其内容将出现在网站的标题或顶部部分。现在运作良好。详细代码https://wordpress.com/post/mamuncse0057.wordpress.com/127

© www.soinside.com 2019 - 2024. All rights reserved.