When using WordPress to build web applications, we often need to use WordPress functionality outside of a theme or plugin. The most common example is running WordPress queries. If you try to call WordPress query functions directly, you will get errors because those core functions have not been loaded yet. We only need to load the relevant WordPress core file.
To load WordPress core functionality, just include the following wp-blog-header.php file.
// 包含WordPress主题的主要文件
define('WP_USE_THEMES', false);
require('/server/path/to/your/wordpress/site/htdocs/blog/wp-blog-header.php');
The path above is the absolute path on the server, so it must be correct.
After that, we can use custom queries in our own program to get WordPress data.
<?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; ?>
