Add “Recent Posts” from a wordpress blog to a html static page

浪尽此生 提交于 2019-12-11 12:29:57

问题


I'm working on a new project and my client needs a site with blog.

But I'm a terrible PHP programmer.. So I created the entire site on HTML/CSS and the blog with wordpress. OK, sounds good! but how to put the "Recent posts" from the blog(wordpress) in my index html page?


回答1:


Method 1 : wp_get_recent_posts()

According to WordPress codex: wp_get_recent_posts() will return a list of posts. Different from get_posts which returns an array of post objects.

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

?>

Method 2 : WordPress loop

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

?>


来源:https://stackoverflow.com/questions/34910629/add-recent-posts-from-a-wordpress-blog-to-a-html-static-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!