我正在做一个新项目,我的客户需要一个带博客的网站。
但我是一个糟糕的PHP程序员。所以我在HTML/CSS上创建了整个网站,并使用wordpress创建了博客。好吧,听起来不错!但是如何将博客(wordpress)中的"最近帖子"放在我的索引HTML页面中?
方法 1 : wp_get_recent_posts()
根据WordPress codex:wp_get_recent_posts()将返回帖子列表。与返回 post 对象数组的 get_posts 不同。
<?php
include('blog/wp-load.php'); // Blog path
// Get the last 5 posts
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 5,
'post_type' => 'post',
'post_status' => 'publish'
));
// Display them as list
echo '<ul>';
foreach($recent_posts as $post) {
echo '<li><a href="', get_permalink($post['ID']), '">', $post['post_title'], '</a></li>';
}
echo '</ul>';
?>
方法2:WordPress循环
<?php
define('WP_USE_THEMES', false);
include('blog/wp-load.php'); // Your blog path
//Get 5 posts
query_posts('showposts=5');
// Display them as list
echo '<ul>';
foreach($recent_posts as $post) {
echo '<li><a href="', the_permalink(), '">', the_title(), '</a></li>';
}
echo '</ul>';
?>