wordpress获取最新文章

十度 wordpress 2015年12月20日 收藏

wordpress经常需要使用到获取最新文章的功能,本文介绍两种方法:

使用query_posts获取最新文章

<ul>
<?php query_posts('showposts=10′); ?><!– 指定文章列表条件 –>
<?php while (have_posts()) : the_post(); ?>
    <li id="post-<?php the_ID(); ?>">
        <span><?php the_time('Y-m-d') ?></span>
        <a href="<?php the_permalink() ?>" ><?php the_title(); ?></a>
    </li>
<?php endwhile; ?>
</ul>

使用wp_get_recent_posts获取最新文章(推荐)

<h2>最新文章</h2>
<ul>
<?php
 $recent_posts = wp_get_recent_posts();
 foreach( $recent_posts as $recent ){
  echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look'.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
 }
?>
</ul>