WordPress中获取最近文章是一种比较常用的功能,在文章推荐或者首页中经常使用,下面推荐两种办法。
1.使用query_posts,这个比较简单,普通的查询。例子如下:
<?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; ?>
2.用wp_get_recent_posts,这个是官方比较推荐的办法,参数比较齐全,案例如下:
<h2>Recent Posts</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>
其实这两种办法没啥不同,读者可以随便使用,如果代码不懂,推荐使用第一种,代码比较熟悉,又比较爱做优化的推荐使用第二种。
本文谢绝转载,本文链接地址: WordPress中获取最近文章的两种办法
声明:WordPress主题站文章谢绝转载,请采集者或恶意抄袭剽窃者自重!本文链接地址: WordPress中获取最近文章的两种办法