繁体   English   中英

从wordpress访问和显示wordpress帖子

[英]access and display wordpress posts from out of wordpress

我有一个商业网站(php),并在一个子目录中有一个Wordpress博客。 我需要在WordPress主页上显示最新帖子:/

网站: http//www.blabla.com

博客: http//www.blabla.com/blog/

所以我需要在www.blabla.com/index.php上发布帖子。 如何访问Wordpress功能?

非常感谢! 欣赏!

最简单的方法是使用Wordpress RSS提要。

使用file_get_contents()cURL下载它以获得更多控制。

simpleXML解析并输出它。

您可能希望将其缓存在某个地方......您可以使用APC用户函数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,因为它是专门为此目的而创建的。

在此之后,使用WP_Query对象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; ?>  

我想最简单的解决方案是直接从数据库中获取帖子。

暂无
暂无

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

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