【发布时间】:2009-09-22 10:48:08
【问题描述】:
我有一个商业网站 (php),并且在子目录中有一个 Wordpress 博客。我需要在主页上显示 Wordpress 之外的最新帖子:/
博客: http://www.blabla.com/blog/
所以我需要在 www.blabla.com/index.php 上显示帖子。如何访问 Wordpress 功能?
非常感谢!欣赏!
【问题讨论】:
我有一个商业网站 (php),并且在子目录中有一个 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>';
}
【讨论】:
使用 WordPress 最佳实践,您不应该加载 wp-blog-header.php,而是加载 wp-load.php,因为它是专门为此目的而创建的。
之后,使用the WP_Query object 或get_posts()。 WordPress codex 上的The Loop 页面上提供了如何使用 WP_Query 的示例。虽然如果您从 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; ?>
希望这会有所帮助! :)
【讨论】:
嘿,刚刚在网上找到了一个解决方案;
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; ?>
【讨论】:
我想最简单的解决方案是直接从数据库中获取帖子。
【讨论】: