探索建站和运维乐趣

如何快速调取WordPress 7天内更新图文文章列表方法

由于我们有些时候在WordPress主题中需要在某个位置侧栏调用最新文章,我们一般是可以直接拖动小工具实现,但是如果有些时候需要指定的调用标题如何设置呢?比如我们需要调用WordPress一周更新的文章列表,这里我们当然也可以使用插件实现,但是在这里我们直接用代码调用。

//调用一周文章列表 by laobuluo.com
function myweek_articles_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(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?> "><?php the_title(); ?></a></li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else:  ?>
<p><?php _e( '没有内容' ); ?></p>
<?php endif;
}

这里添加到Functipns.php中,然后调用。

<?php myweek_articles_week(); ?>

如果我们需要修改样式,可以在上面代码里修改,比如增加摘要或者特色图片等。

赞(0)
转载保留:老部落 » 如何快速调取WordPress 7天内更新图文文章列表方法


关注公众号『老蒋朋友圈』

获取更多建站运营运维新知!
互联网创业、前沿技术......