外部调用Wordpress文章

通过调用 wp-load.php 文件获取wordpress主要功能的。

wp-load.php加载了Wordpress本身和它所有的程序开发接口(API),装载后就可以在自己的程序中调用wordpress的函数。

博客网站根目录下创建php文件,此处命名为output.php。内容如下


<?php
// Include WordPress
define('WP_USE_THEMES', false);
require('./wp-load.php');
query_posts('showposts=10');
//这个调用最新文章,如果是热门文章的话则改为get_most_viewed("post",10),前提是你的主题安装了热门文章插件,而且此方法可以接受几乎wp-kit-cn所有代码。
?>
<?php while (have_posts()): the_post(); ?>
<li><a href="<?php the_permalink(); ?>" target="_blank"><?php echo mb_strimwidth(strip_tags(apply_filters('the_title', $post->post_title)), 0, 50," "); ?></a></li>
<?php endwhile; ?>


上面的代码可以输出文章标题

下面的代码则可以输出自定摘要


<?php
// Include WordPress
define('WP_USE_THEMES', false);
require('./wp-load.php');
query_posts('showposts=30');
?>
<?php while (have_posts()): the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>


在调用博客文章的地方插入以下代码,即可调用相应的内容


<?php
$url=http://yourblogurl/output.php;
echo file_get_contents( $url );
?>


上一篇:换主机导致WordPress内部链接失效解决办法

下一篇:记录一下近期的生活,有一年不更新了