wordpress显示摘要_如何在WordPress中显示上周的帖子

wordpress显示摘要

Many of our beginner level readers soon start modifying their WordPress themes thats why we have a WordPress theme cheat sheet to help them get started. This brings some interesting challenges for new users. One such reader, recently asked us how to display last week’s posts in WordPress. They just wanted to add a section on their home page which displayed posts from previous week. In this article, we will show you how to display last week’s posts in WordPress.

我们的许多初学者读者很快就会开始修改WordPress主题,这就是为什么我们拥有WordPress主题备忘单来帮助他们入门的原因。 这给新用户带来了一些有趣的挑战。 其中一位读者最近问我们如何在WordPress中显示上周的帖子。 他们只是想在首页上添加一个部分,以显示上周的帖子。 在本文中,我们将向您展示如何在WordPress中显示上周的帖子。

Before we show you how to display previous week’s posts, let’s first take a look at how you can display current week’s posts using WP_Query. Copy and paste the following code in your theme’s functions.php file or a site-specific plugin.

在向您展示如何显示上周的帖子之前,让我们首先看一下如何使用WP_Query显示本周的帖子。 将以下代码复制并粘贴到主题的functions.php文件或特定站点的插件中


function wpb_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="Permanent link to <?php the_title(); ?> "><?php the_title(); ?></a></h2>
	<?php the_excerpt(); ?>
  <?php endwhile; ?>
  <?php wp_reset_postdata(); ?>
<?php else:  ?>
  <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
}

In the example code above, we first found out the current week and year. We then used those values in WP_Query to display posts from current week. Now all you need to do is add <?php wpb_this_week(); ?> in your theme file where you want to display the posts.

在上面的示例代码中,我们首先找出当前的星期和年份。 然后,我们在WP_Query中使用这些值来显示当前星期的帖子。 现在您需要做的就是添加<?php wpb_this_week(); ?> <?php wpb_this_week(); ?>在主题文件中要显示帖子的位置。

This was simple, wasn’t it? Now to display last week’s posts all you need to do is minus 1 from the week’s value. But if this is the first week of the year, then you will get 0 for the week and current year instead of last year. Here is how you fix that problem.

这很简单,不是吗? 现在要显示上周的帖子,您需要做的就是将一周的值减去1。 但是,如果这是一年中的第一周,那么该周和当前年份(而不是去年)将为0。 这是解决此问题的方法。



function wpb_last_week_posts() { 
$thisweek = date('W');
if ($thisweek != 1) :
$lastweek = $thisweek - 1;   
else : 
$lastweek = 52;
endif; 
$year = date('Y');
if ($lastweek != 52) :
$year = date('Y');
else: 
$year = date('Y') -1; 
endif;
$the_query = new WP_Query( 'year=' . $year . '&w=' . $lastweek );
if ( $the_query->have_posts() ) : 
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <h2><a href="<?php the_permalink(); ?>" title="Permanent link to <?php the_title(); ?> "><?php the_title(); ?></a></h2>
	<?php the_excerpt(); ?>
  <?php endwhile; ?>
  <?php wp_reset_postdata(); ?>
<?php else:  ?>
  <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;

}

In the sample code above we have placed two checks. The first check sets the last week’s value to 52 (which is the last week in a year) when the current week’s value is 1. The second check sets year’s value to last year when the last week’s value is 52.

在上面的示例代码中,我们进行了两次检查。 当当前星期的值为1时,第一张检查将最后一周的值设置为52(这是一年中的最后一周),第二张检查将上周的值设为52,则将年份的值设置为去年。

To display last week’s posts all you need to do is add <?php wpb_last_week_posts(); ?> to your theme’s template file where you would like to display them. Or if you would like to have a shortcode so that you can add this into a page or a widget, then simply add this line below the code given above.

要显示上周的帖子,您只需添加<?php wpb_last_week_posts(); ?> <?php wpb_last_week_posts(); ?>到您想要显示主题的模板文件。 或者,如果您想要一个短代码以便可以将其添加到页面或窗口小部件中,则只需在上面给出的代码下方添加此行。


add_shortcode('lastweek', 'wpb_last_week_posts');

You can now use this shortcode in a post, page, or a widget like this:

现在,您可以在帖子,页面或此类小部件中使用此短代码:

[lastweek]

[lastweek]

Please note, that you don’t always need WP_Query to create custom queries. WordPress comes with a handful of functions to help you display recent posts, archives, comments, etc. If there is an easier way to use the existing functions, then you don’t really need to write your own queries.

请注意,您并非始终需要WP_Query来创建自定义查询。 WordPress附带了一些功能来帮助您显示最新的帖子档案评论等。如果有使用现有功能的简便方法,那么您实际上就不需要编写自己的查询。

We hope this article helped you display last week’s posts in WordPress. Experiment with the code and modify it to meet your needs. Let us know if you have any questions by leaving a comment below or join us on Twitter.

我们希望本文能帮助您显示WordPress上周的帖子。 试用代码并对其进行修改以满足您的需求。 如果您有任何疑问,请在下面发表评论或在Twitter上加入我们,以告诉我们。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-display-last-weeks-posts-in-wordpress/

wordpress显示摘要

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值