返回而不是回声在WP插件中

这可能是一个愚蠢的问题,但我是编码新手,所以这里是:-)。我正在尝试为WordPress创建一个简单的插件。该插件从MySQL数据库获取数据,并回显出带有结果的表。我的问题是,当我使用echo时,插件是页面上的第一位,即使我把短代码放在页面的中间。我理解这是因为我使用回声而不是返回。我只是不知道如何在我的情况下使用回报。任何帮助将不胜感激:-)。这是我的代码:


    $get_runners = $connection->prepare('SELECT first_name, last_name, nick_name, FROM database WHERE status = :status ORDER BY first_name ASC');

    $get_runners->execute([status=>'success']);


    // Create the table

    echo '

        <table id="Table" class="start-list-table">

            <thead>

                <tr class="start-list-tr">

                    <th scope="col">Name</th>

                    <th scope="col">Club</th>

                </tr>

            </thead>

            <tbody>

    ';

    // Get the runner object:

    $runners = $get_runners->fetchAll();

    foreach($runners as $runner){

        if($runner->nick_name)

        {

            $runner_name = $runner->first_name.' "'.$runner->nick_name.'" '.$runner->last_name;

        }

        else

        {

            $runner_name = $runner->first_name.' '.$runner->last_name;

        }

        echo '

            <tr class="start-list-tr">

                <td data-label="Name">'.$runner_name.'</td>

                <td data-label="Club">'.$runner->club.'</td>

            </tr>';

    }

    echo '</tbody>

    </table>';

}

add_shortcode( 'startlist', 'create_startlist' );


婷婷同学_
浏览 73回答 2
2回答

米琪卡哇伊

您希望将输出分配给变量,而不是回显:$get_runners = $connection->prepare('SELECT first_name, last_name, nick_name, FROM database WHERE status = :status ORDER BY first_name ASC');&nbsp; &nbsp; $get_runners->execute([status=>'success']);&nbsp; &nbsp; // Create the table&nbsp; &nbsp; $output = '&nbsp; &nbsp; &nbsp; &nbsp; <table id="Table" class="start-list-table">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr class="start-list-tr">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Club</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tbody>&nbsp; &nbsp; ';&nbsp; &nbsp; // Get the runner object:&nbsp; &nbsp; $runners = $get_runners->fetchAll();&nbsp; &nbsp; foreach($runners as $runner){&nbsp; &nbsp; &nbsp; &nbsp; if($runner->nick_name)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $runner_name = $runner->first_name.' "'.$runner->nick_name.'" '.$runner->last_name;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $runner_name = $runner->first_name.' '.$runner->last_name;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $output .= '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr class="start-list-tr">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td data-label="Name">'.$runner_name.'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td data-label="Club">'.$runner->club.'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>';&nbsp; &nbsp; }&nbsp; &nbsp; $output .= '</tbody>&nbsp; &nbsp; </table>';&nbsp;return $output;}add_shortcode( 'startlist', 'create_startlist' );这使用串联来继续通过函数填充变量。然后,将 设置为变量。return$output

慕盖茨4494581

首先阅读更多关于短代码输出:https://codex.wordpress.org/Shortcode_API#Output此刻我能想到两种方式。正在使用ob_start...基本上你需要把你的代码包装进去ob_start()function create_startlist() {&nbsp; &nbsp; ob_start();&nbsp; &nbsp; &nbsp;/* CODE HERE */&nbsp; &nbsp; return ob_get_clean();}二是使用串联运算符function create_startlist() {&nbsp; &nbsp; $output = '';&nbsp; &nbsp; $output .= 'OUTPUT HERE';&nbsp; &nbsp; return $output;}
打开App,查看更多内容
随时随地看视频慕课网APP