在 WordPress 外部运行循环

Avatar of Chris Coyier
Chris Coyier

包含基本 WordPress 函数

<?php
  // Include WordPress
  define('WP_USE_THEMES', false);
  require('/server/path/to/your/wordpress/site/htdocs/blog/wp-blog-header.php');
  query_posts('showposts=1');
?>

运行循环

<?php while (have_posts()): the_post(); ?>
   <h2><?php the_title(); ?></h2>
   <?php the_excerpt(); ?>
   <p><a href="<?php the_permalink(); ?>" class="red">Read more...</a></p>
<?php endwhile; ?>

这可以在任何 PHP 文件中使用,即使在 WordPress 安装外部。