我需要在非wordpress php页面中显示wordpress博客帖子。我试过以下代码。
<?php
// Include WordPress
define('WP_USE_THEMES', false);
//exact path for wp-load.php.
// This file is kept in the root of wordpress install
require('http://test.com/wordpress/blog/wp-load.php');
//Query wordpress for latest 4 posts
query_posts('showposts=5');
?>
<?php while (have_posts ()): the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
但它显示了以下错误
Fatal error: Call to undefined function query_posts()
如何解决这个问题?
答案 0 :(得分:3)
请在代码中查看以下行
require('http://test.com/wordpress/blog/wp-load.php');
在require函数中,您应该使用相对或物理路径。你不应该包括网址。
答案 1 :(得分:1)
由于你需要wordpress数据库和框架,这似乎根本不可能工作。尝试从wordpress获取XML,RSS或JSON数据,您可以使用自制脚本获取它。
答案 2 :(得分:1)
对于外部集成,通过RSS路由处理此问题可能更可靠。使用 simplexml_load_file (以及HTTP流)最简单且可能最懒惰的方法。
$t = simplexml_load_file( "http://blogs.voanews.com/breaking-news/feed/" );
foreach( $t->channel->item as $item ) {
printf(
"<div>%s <a href='%s'>%s</a></div><hr/>",
$item->description,
$item->link,
$item->title
);
}
这会根据您的预期输出Feed。请注意,这不会使用任何类型的缓存,因此每个页面请求都会触及原始Feed。
<div>Some Chinese officials are furious at Apple's iPhone for apparently
helping users have too much of a good time. Chinese media say the complaints
surround the iPhone's voice-activated personal assistant, known as
“Siri,” which has been helping some users find prostitutes and
brothels. The Mandarin language version can apparently present users with as
many as [...] <a href='...(snip)...)'>iPhone Under Fire in China over
Prostitution</a></div>
答案 3 :(得分:0)
您可以使用rss选项,您可以使用该文件编写新的隐藏代码并以JSON格式读取数据
也许首先要做的是搜索一个为你做这个的扩展/插件/模块;
你不是第一个想要这样做的人,我想:p
答案 4 :(得分:0)
要在wordpress设置之外显示最近的帖子,首先要包含wp-load.php文件。
require( './blog/wp-load.php' );
// Load the recent top 10 posts
$args = array( 'posts_per_page' => 10, 'post_status'=>"any", 'post_type'=>"post", 'orderby'=>"date","order"=> "DESC", "suppress_filters"=>true);
$postslist = get_posts( $args );
现在你可以遍历$ postlist变量了。
foreach ($postslist as $post) : setup_postdata($post);?>
<ul class="media-list">
<li class="media">
<div class="media-left"> <?php the_post_thumbnail( array(80,80));?> </div>
<div class="media-body">
<a target="_blank" href="<?php the_permalink();?>"><h5 class="media-heading"><?php the_title(); ?></h5></a>
<p><?php echo $post->post_content; ?></p>
</div>
</li>
</ul>
<?php endforeach;
答案 5 :(得分:0)
在包含wp-load.php后,您必须像这样实例化查询:
$wp_query = new \WP_Query();
$wp_query->query('showposts=5');
之后你的循环应该是这样的:
<?php while ($wp_query->have_posts()) :
$wp_query->the_post();
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>