将“最近的帖子”从wordpress博客添加到html静态页面

时间:2016-01-20 21:12:51

标签: php html wordpress posts

我正在开发一个新项目,我的客户需要网站 博客

但我是一个糟糕的PHP程序员..所以我在HTML / CSS和使用wordpress的博客上创建了整个网站。 好的听起来不错!但如何在我的索引html页面中将博客(wordpress)中的“近期帖子”放入?

1 个答案:

答案 0 :(得分:2)

方法1:wp_get_recent_posts()

根据WordPress codex:wp_get_recent_posts()将返回帖子列表。与get_posts不同,后者返回一个post对象数组。

<?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>';

?>
相关问题