在wordpress安装目录之外加载wordpress博客文章

时间:2018-10-17 09:10:09

标签: php wordpress

是否可以仅将某些内容加载到wordpress安装目录之外?我正在一个网站上,我想将一些博客内容嵌入到使用bootstrap4制作的静态index.php上。该页面不是wordpress安装的一部分,因此我想了解这是否可行以及如何实现。

1 个答案:

答案 0 :(得分:0)

尝试一下:

注意:将“ 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();
相关问题