Load wordpress blog posts outside wordpress instal

2019-09-20 19:27发布

This question already has an answer here:

Is it possible to load only some contents outside the wordpress installation directory? I'm working on a website and I want to embed some blog contents on a static index.php made using bootstrap4. This page is not part of the wordpress installation, so I want to understand if this is possible and how.

标签: php wordpress
1条回答
冷血范
2楼-- · 2019-09-20 19:36

Try this:

Note: change the 'path-to-wordpress' to actual wp directory path.

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();
查看更多
登录 后发表回答