关于WordPress相关文章、最新文章的调用我们应该还好办,比如默认的系统就支持调用最新文章的模块,以及相关文章我们可以使用插件或者无插件调用。但是,如果我们需要调用上一周网站更新的内容如何实现呢?因为公司某个网站需要调用上一周的内容出来,我们就找找看看有没有非插件实现的方法。
如果使用插件很简单,这里我不用插件,于是找到一个不错的方法。
第一、调用上一周文章代码
- functionwpweek_this_week(){
- $week=date(‘W’);
- $year=date(‘Y’);
- $the_query=newWP_Query(‘year=’.$year.‘&w=’.$week);
- if($the_query->have_posts()):
- while($the_query->have_posts()):$the_query->the_post();?>
- <h2><ahref=“<?phpthe_permalink();?>”title=“<?phpthe_title();?>“><?phpthe_title();?></a></h2>
- <?phpthe_excerpt();?>
- <?phpendwhile;?>
- <?phpwp_reset_postdata();?>
- <?phpelse:?>
- <p><?php_e(‘没有合适的内容’);?></p>
- <?phpendif;
- }
将代码添加到当前主题的Functions.php文件中。
第二步、调用文章
- <?phpwpweek_this_week();?>
然后在需要调用的位置,输入上面代码就可以。