简体   繁体   English

从wordpress访问和显示wordpress帖子

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

I have a commercial site (php), and have a Wordpress blog in a subdirectory. 我有一个商业网站(php),并在一个子目录中有一个Wordpress博客。 I need to display latest posts at homepage which is out of Wordpress :/ 我需要在WordPress主页上显示最新帖子:/

site: http://www.blabla.com 网站: http//www.blabla.com

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

So I need to display posts at www.blabla.com/index.php. 所以我需要在www.blabla.com/index.php上发布帖子。 How can I access Wordpress functionality? 如何访问Wordpress功能?

Thanks a lot! 非常感谢! appreciate! 欣赏!

The easiest way is to consume your Wordpress RSS feed. 最简单的方法是使用Wordpress RSS提要。

Download it using file_get_contents() or cURL for more control. 使用file_get_contents()cURL下载它以获得更多控制。

Parse it with simpleXML and output it. simpleXML解析并输出它。

You'll probably want to cache it somewhere... you could use APC user functions or PEAR::Cache_Lite . 您可能希望将其缓存在某个地方......您可以使用APC用户函数PEAR :: Cache_Lite

Edit: the code would look something like this (you'd want more error checking and stuff - this is just to get you started): 编辑:代码看起来像这样(你想要更多的错误检查和东西 - 这只是为了让你开始):

$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>';
}

Using WordPress best practices, you shouldn't be loading wp-blog-header.php, but rather wp-load.php, as it was specifically created for this purpose. 使用WordPress最佳实践,您不应该加载wp-blog-header.php,而是加载wp-load.php,因为它是专门为此目的而创建的。

After this, use either the WP_Query object or get_posts() . 在此之后,使用WP_Query对象get_posts() An example of how to use WP_Query is available on The Loop page on the WordPress codex. WordPress codex的The Loop页面上提供了如何使用WP_Query的示例。 Although using either of these doesn't matter if you use them from outside WordPress, there's less chance of something interfering, such as GET parameters. 虽然使用其中任何一个都无关紧要,如果你从WordPress外部使用它们,那么干扰的可能性就会降低,例如GET参数。

For example, using WP_Query: 例如,使用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; ?>

Or, using get_posts(): 或者,使用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; ?>

Hope this helps! 希望这可以帮助! :) :)

hey just found a solution online; 嘿刚刚在线找到解决方案;

http://www.corvidworks.com/articles/wordpress-content-on-other-pages http://www.corvidworks.com/articles/wordpress-content-on-other-pages

works great! 效果很好!

<?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