php - 在 wordpress 安装目录之外加载 wordpress 博客文章
问题描述
是否可以仅加载 wordpress 安装目录之外的一些内容?我在一个网站上工作,我想在index.php
使用 bootstrap4 制作的静态文件中嵌入一些博客内容。此页面不是 wordpress 安装的一部分,所以我想了解这是否可能以及如何。
解决方案
尝试这个:
注意:将 '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();
推荐阅读
- javafx - 为什么编译器在使用 TableColumn 时会生成未经检查的警告
而不是 TableColumn在 JavaFX8 TableView 中选择一个单元格? - python - Google Cloud Functions - 如何对 AWS S3 存储桶进行身份验证?
- reactjs - 单击提交按钮后变量不会立即更新
- php - 如何删除json_encode数组的双引号
- google-sheets - 根据工作年限计算应计季度津贴的公式
- django - Django clean 在非必填字段上运行
- google-apps-script - 我每 2 分钟就会收到一封烦人的电子邮件
- wordpress - 图像/媒体未使用高级自定义字段加载
- javascript - Discord Markdown 与 Discord.js
- c# - Photon PUN2 Unity3D - 发送 RPC 可在独立 (PC) 上工作,但不适用于 Android