我有一個商業網站(PHP),並有一個WordPress的博客在子目錄中。我需要顯示在主頁最新的帖子這超出的WordPress:/訪問和顯示WordPress的帖子出來
博客: http://www.blabla.com/blog/
所以我需要在www.blabla.com/index顯示帖子。 PHP。我怎樣才能訪問WordPress的功能?
非常感謝!欣賞!
我有一個商業網站(PHP),並有一個WordPress的博客在子目錄中。我需要顯示在主頁最新的帖子這超出的WordPress:/訪問和顯示WordPress的帖子出來
博客: http://www.blabla.com/blog/
所以我需要在www.blabla.com/index顯示帖子。 PHP。我怎樣才能訪問WordPress的功能?
非常感謝!欣賞!
最簡單的方法是使用您的Wordpress RSS供稿。
使用file_get_contents()
或cURL下載更多控件。
用simpleXML解析並輸出。
您可能想將它緩存在某處......您可以使用APC user functions或PEAR::Cache_Lite。
編輯:代碼會是這個樣子(你想要更多的錯誤檢查和東西 - 這僅僅是讓你開始):
$xmlText = file_get_contents('http://www.blabla.com/blog/feed/');
$xml = simplexml_load_string($xmlText);
foreach ($xml->item as $item)
{
echo 'Blog Post: <a href="' . htmlentities((string)$item->link) . '">'
. htmlentities((string)$item->title) . '</a>';
echo '<p>' . (string)$item->description . '</p>';
}
嘿剛剛在網上找到了一個解決方案;
http://www.corvidworks.com/articles/wordpress-content-on-other-pages
的偉大工程!
<?php
// Include Wordpress
define('WP_USE_THEMES', false);
require('blog/wp-blog-header.php');
query_posts('showposts=3');
?>
<?php while (have_posts()): the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php endwhile; ?>
使用WordPress的最佳實踐,你不應該裝WP-博客 - 的header.php,而是WP-load.php,因爲它用於此目的專門創建的。
在此之後,使用the WP_Query object或get_posts()。有關如何使用WP_Query的示例,請參見WordPress代碼上的The Loop頁面。儘管如果您使用WordPress以外的其中任何一種都不重要,那麼幹擾的可能性就會降低,例如GET參數。
例如,使用WP_Query:
<?php
$my_query = new WP_Query('showposts=3');
while ($my_query->have_posts()): $my_query->the_post();
?>
<h1><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h1>
<?php endwhile; ?>
或者,使用get_posts():
<?php
global $post;
$posts = get_posts('showposts=3');
foreach($posts as $post) :
?>
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<?php endforeach; ?>
希望這有助於! :)
uuu它聽起來不是那麼容易其實:)很多步驟...我正在研究你的建議的細節,謝謝! – 2009-09-22 10:57:59
@artmania:由於你的主站點和你的博客都在同一臺服務器上,並且都使用php,所以這種技術可能是不必要的,儘管在某些方面它比你所做的要靈活一點 – Brian 2009-09-22 11:50:22