访问和显示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