记录一些常用的函数和瞎折腾的过程……

wordpress调用最新文章

提醒:本文最后更新于 880 天前,文中所描述的信息可能已发生改变,请谨慎使用。

在制作wordpress主题时,可能需要在侧边栏调用最新文章,方法很多,我经常使用的2个方法如下,调用基本都很简单,只需要几个少量的参数就能搞定:

方法一:使用wp_get_archives归档调用:

<?php wp_get_archives('type=postbypost&format=html&show_post_count=1&limit=8');?>

方法二:使用WP_Query查询调用:

<?php
$my_query=new WP_Query(
  array(
    'post_type'=>'post',
    'posts_per_page'=>10,
    'orderby'=>'date',
    'order'=>'DESC'
    )
  );
  if($my_query->have_posts()):while($my_query->have_posts()):$my_query->the_post();
?>
    <li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>
  <?php endwhile; endif; ?>