2014-07-02 31 views
0

我的WordPress的網站是由我自己寫的插件放慢。該插件從不同站點上的RSS源加載事件。懶惰加載一個RSS提要在PHP

我禁用了該插件,它在Google Page Speed中的差異爲20。

我該如何在Ajax或Javascript中加載RSS feed?

的代碼,我有:

$rss = simplexml_load_file(get_option('capu_url')); 

foreach ($rss->channel->item as $item) { 
    echo '<h4><a href="'. $item->link .'">' . $item->title . "</a></h4>"; 
//echo "<p>" . $item->description . "</p>"; 


$dom = new DOMDocument; 
$dom->strictErrorChecking = FALSE ; 
libxml_use_internal_errors(true); 
$dom->loadHTML($item->description); 
$xpath = new DOMXPath($dom);      
$nodes = $xpath->query('//ul[@class="ee-event-datetimes-ul"]'); // get <ul>'s with class 'up' 
foreach($nodes as $node) {      // loops through each <ul> 
    foreach($node->getElementsByTagName('li') as $li) { // loops through the <li>'s 

    echo $li->nodeValue . "<br/>\n"; // echo's the <li> elements 
+0

你能告訴我們你已經有的代碼嗎? –

+0

當然,我會馬上添加它 –

回答

3

根據您必須將服務器的訪問,考慮運行cron作業,例如一小時一次,以獲取RSS源,將其轉換成JSON並保存作爲web根目錄中的某個文件。

現在,您可以使用JavaScript從服務器異步檢索提要作爲JSON數據文件並將其顯示在您的網站上。

如果您沒有shell訪問權限,請嘗試在Wordpress中使用wp_cron函數。

+0

謝謝,這的確是一個很好的解決方案,在加速小部件 –

+0

智能解決方案:) –