首页 > 解决方案 > 在 wordpress 安装目录之外加载 wordpress 博客文章

问题描述

是否可以仅加载 wordpress 安装目录之外的一些内容?我在一个网站上工作,我想在index.php使用 bootstrap4 制作的静态文件中嵌入一些博客内容。此页面不是 wordpress 安装的一部分,所以我想了解这是否可能以及如何。

标签: phpwordpress

解决方案


尝试这个:

注意:将 'path-to-wordpress' 更改为实际的 wp 目录路径。

define('WP_USE_THEMES', FALSE);
require('/path-to-wordpress/wp-load.php');

$args = array(
  'posts_per_page' => 5 // Specify how many posts you'd like to display
);
$latest_posts = new WP_Query( $args );

if ( $latest_posts->have_posts() ) {
  while ( $latest_posts->have_posts() ) {
  $latest_posts->the_post(); ?>
  <li>
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    <?php the_excerpt(); ?>
  </li>
<?php }
}
else {
  echo '<p>There are no posts</p>';
}
wp_reset_postdata();
wp_reset_query();

推荐阅读