将函数转换为 PHP 简码


converting a function into a php shortcode

我是PHP WordPress简码的新手,我在创建可以插入到我的内容中的简码时遇到了问题。我正在创建的函数/短代码没有输入值。问题是,当我在内容中使用它时,所有内容似乎都消失了,并且功能中没有显示任何内容。当我尝试回显它但不是通过短代码时,该函数会自行工作。我做错了什么

getHTML 正在调用另一个函数,但没有理由添加它,因为它有效。希望得到一些关于我做错了什么的指导,以使这个简码工作

include(TEMPLATEPATH . '/simple_html_dom.php');
function getGames() {

$html = str_get_html(getHTML('http://URL',10));
    //$title = str_replace(array("'n", "'r"), '',$html->find("/[@id='main']/div[1]/div[2]/div[1]/h2/strong",0)->plaintext);
    //$manuf = $html->find("/[@id='main']/div[1]/div[2]/div[3]/table/tbody/tr[1]/td[2]/strong",0)->plaintext;
$table = $html->find("/[@id='matches_list']/",0);

$livescore = '';
            $livescore .= "<table class='match-table' cellspacing='0' cellpadding='0'>";
             $livescore .= "<tbody>";
foreach($table->find("li") as $line){
    $game = $line->find("a/span/img",0)->title;
    if(  $game == "CS:GO" or $game == "Hearthstone" or $game == "Dota 2" or $game == "StarCraft II" or $game == "League of Legends"){
        $opp1 = $line->find("span.opp1",0)->plaintext;
        $opp2 = $line->find("span.opp2",0)->plaintext;
        $score = $line->find("span.score",0)->plaintext;

            $livescore .= "<tr class='margin-tr'>";
            $livescore .= "<td class='match-icon'>IC</td>";
            $livescore .= "<td class='match-home'>". $opp1 ."</td>";
            $livescore .= "<td class='match-score'>". $score ."</td>";
            $livescore .= "<td class='match-away'>". $opp2 ."</td>";
            $livescore .= "<td class='match-time'>22:00</td>";
            $livescore .= "</tr>";
        }
    }
                    $livescore .= "</tbody>";
            $livescore .= "</table>";

    return $livescore;
}

add_shortcode('getGames', 'getGames');

根据简码 API:

简码名称应全部小写并使用所有字母,但数字和下划线也应该正常工作。小心使用连字符(破折号(,最好不要使用它们。

将您的呼叫更改为:

add_shortcode('games', 'getGames');

然后就这样称呼它:

[games]