
wordpress显示摘要
Many of our beginner level readers soon start modifying their WordPress 主题 thats why we have a WordPress 主题备忘单 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 函数.php file or a 特定于站点的插件.
在向您展示如何显示上周的帖子之前,让我们首先看一下如何使用WP_Query显示本周的帖子。 将以下代码复制并粘贴到主题的函数.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(); ?>
在主题文件中要显示帖子的位置。
这很简单,不是吗?现在要显示上周的帖子,您只需将本周的值减 1 即可。但如果这是一年中的第一周,那么该周和当年的值将为 0,而不是去年的值。以下是解决该问题的方法。
这很简单,不是吗?现在要显示上周的帖子,您所需要做的就是从本周的值中减去 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;
}
在上面的示例代码中,我们放置了两项检查。当当前周的值为 1 时,第一个检查将上周的值设置为 52(一年中的最后一周)。当上周的值为 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');
您现在可以在帖子、页面或小部件中使用此短代码,如下所示:
现在您可以在帖子、页面或此类小部件中使用此短代码:
[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 最近的帖子, 档案, 评论, 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 推特.
我们希望本文能帮助您显示WordPress上周的帖子。 试用代码并对其进行修改以满足您的需求。 如果您有任何疑问,请在下面发表评论或在在推特上加入我们,以告诉我们。
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-display-last-weeks-posts-in-wordpress/
wordpress显示摘要