WordPress调用上一周文章内容列表的方法

wp是能不用插件就不用插件,关于WordPress相关文章、最新文章的调用我们应该还好办,比如默认的系统就支持调用最新文章的模块,以及相关文章我们可以使用插件或者无插件调用。但是,如果我们需要调用上一周网站更新的内容如何实现呢?因为公司某个网站需要调用上一周的内容出来,大鸟就找找看看有没有非插件实现的方法。

如果使用插件很简单,这里我不用插件,于是找到一个不错的方法。

第一、调用上一周文章代码

function wpweek_this_week() {
$week = date(‘W’);
$year = date(‘Y’);
$the_query = new WP_Query( ‘year=’ . $year . ‘&w=’ . $week );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?> “><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else:  ?>
<p><?php _e( ‘没有合适的内容’ ); ?></p>
<?php endif;
}

将代码添加到当前主题的Functions.php文件中。

第二步、调用文章

<?php wpweek_this_week(); ?>

然后在需要调用的位置,输入上面代码就可以。


【AD】DMIT,美国CMIN2 VPS,8折优惠,折后$23.10/季(1核2G/20G SSD /[email protected]流量),三网回程CMIN2

【AD】美国洛杉矶CN2 VPS/香港CN2 VPS/日本CN2 VPS推荐,延迟低、稳定性高、免费备份_搬瓦工vps