开放的编程资料库

当前位置:我爱分享网 > PHP教程 > 正文

在 WordPress 之外创建一个“最近的帖子”模块

我从未使用 WordPress 创建过完整的客户网站,但其中许多网站都有 WordPress 博客来补充网站。通常,客户希望在网站内列出最近的博客文章,而不是在 WordPress 外壳内。没问题——创建一个最近的帖子模块,或者任何基于查询的 WordPress 帖子使用,都非常简单!

PHP

在 WordPress 之外使用 WordPress 的关键是包含 wp-load.php 文件:

// Include the wp-load'er
include('wp-load.php');

// Get the last 10 posts
// Returns posts as arrays instead of get_posts' objects
$recent_posts = wp_get_recent_posts(array(
	'numberposts' => 10
));
	
// Do something with them
echo '
    '; foreach($recent_posts as $post) { echo '
  • ', $post['post_title'], '
  • '; } echo '
';

包含wp-load.php 文件后,WordPress 的所有功能都将提供给您。虽然上面的示例代码获取最近的帖子,但您可以使用 WP_Query 或 get_posts() 执行您自己的自定义查询。

未经允许不得转载:我爱分享网 » 在 WordPress 之外创建一个“最近的帖子”模块

感觉很棒!可以赞赏支持我哟~

赞(0) 打赏