繁体   English   中英

将函数转换为php简码

[英]converting a function into a php shortcode

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

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');

根据Shortcode API

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

将您的通话更改为:

add_shortcode('games', 'getGames');

然后通过以下方式调用它:

[games]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM