小工具      在线工具  汉语词典  dos游戏  css  js  c++  java

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

java,wordpress,php,github,python 额外说明

收录于:17天前

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显示摘要

. . .

相关推荐

额外说明

在Java8中,foreach()不能中断。如果我需要继续,我该怎么办?

结论: 在Java8的foreach()中不能break,如果需要continue时,可以使用return。lambda表达式中使用return时,这个方法是不会返回的,而只是执行下一次遍历。 测试代码: List<String> list = Ar

额外说明

启动redis后台服务

    [root@localhost redis-4.0.2]# cd utils/   [root@localhost utils]# ./install_server.sh     查看启动的redis服务 [root@localhost util

额外说明

远程代码执行漏洞—— CVE-2020-11975 Apache Unomi

QQ 1274510382 Wechat JNZ_aming 商业联盟 QQ群538250800 技术搞事 QQ群599020441 解决方案 QQ群152889761 加入我们 QQ群649347320 共享学习 QQ群674240731 纪年科技am

额外说明

Android Studio新手教程

原文章  Android Studio 下载 选择从 Android Studio 中文组 官网进行下载:http://www.android-studio.org/ 如果电脑网络好,能翻墙的,可以直接从官网首页推荐的 google 正版链接进行下载,如

额外说明

Python爬虫之Scrapy框架系列(21)——重写媒体管道类实现保存图片名字自定义及多页爬取

目录: 重写框架自带媒体管道类部分方法实现保存图片名字的自定义: 1.爬虫文件: 2.items.py文件中设置特殊的字段名: 3.settings.py文件中开启自建管道并设置文件存储路径: 4.编写pipelines.py 5.观察可发现完美实现:

额外说明

C#的数据集:DataSet对象

目录 一、合并DataSet内容 1.源码 2.生成效果  二、 复制DataSet内容 1.源码 2.生成效果         DataSet对象就像存放于内存中的一个小型数据库。它可以包含数据表、数据列、数据行、视图、约束以及关系。通常,DataSe

额外说明

ctrl点击HttpServlet无法查看源码解决办法

需要下载外部资源导入开发平台才能查看。 在eclipse中若出现点击HttpServlet想查看源码时出现  进入Apache Tomcat主页:http://tomcat.apache.org/ 点击左侧download  tomcat是多少版本的就点

额外说明

[Ext JS6实战]动态数据绑定

文章目录 问题提出 问题解决 实例代码 关于Ext JS的View Model,参见: https://blog.csdn.net/oscar999/article/details/85310641 问题提出 使用ViewModel的data属性可以绑定

额外说明

线程

线程 线程的理解 要想了解线程,就需先了解进程,进程是静态的,比如如何让我们编完的代码运行起来,因为静态代码是放在代码区的。如果想要它跑起来,我们一般用到的是: public static void main(String[] args){ S

额外说明

【linux】linux 报错:安装 nginx 时,make 报错解决方法

目录 一、报错情况 二、解决方法 三、openssl旧版本下载和安装 一、报错情况 (1)输入命令,报错: make (2)具体报错: make[1]: *** [objs/src/event/ngx_event_openssl.o] 错误 1 make

ads via 小工具