我从未使用 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() 执行您自己的自定义查询。
