热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Wordpress:仅显示未来的帖子减去一天-Wordpress:Onlyshowfuturepostsminusoneday

SoIhavealoopthatrunsperfectforeventsandonlyshowsfutureposts.TheissueisthatIwould

So I have a loop that runs perfect for events and only shows future posts. The issue is that I would love to keep posts that are no longer future posts in the loop for an extra day.

所以我有一个完美的活动循环,只显示未来的帖子。问题是,我希望在循环中保留不再是未来帖子的帖子一天。

Example: So if the event (or post that is scheduled) is for 8pm on the 3rd. As of now it is removed at 8pm (which is an issue because it might last for 4 hours).

示例:因此,如果事件(或计划的帖子)是在3日晚上8点。截至目前,它已于晚上8点被删除(这是一个问题,因为它可能会持续4个小时)。

I would like posts to remain up for an extra day, or time that I would be able to alter.

我希望帖子可以保留一天,或者我可以改变的时间。

Here is my current code:

这是我目前的代码:

 'event', 'posts_per_page' => 50, 'post_status' => 'future', 'order' => 'ASC' );
                    $loop = new WP_Query( $args );
                    if ( have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();?>
                        
                        
                        

2 个解决方案

#1


3  

It seems like the time parameters for WP_Query are able to specify definite time spans but not indefinite ones, e.g. posts from now into the future. The WordPress docs recommends using the posts_where filter for time-relative queries. So you might put this in your theme's functions.php:

看起来WP_Query的时间参数能够指定明确的时间跨度,但不能指定不确定的时间跨度,例如从现在到未来的帖子。 WordPress文档建议使用posts_where过滤器进行时间相关查询。所以你可以把它放在你的主题的functions.php中:

// Create a new filtering function that will add our where clause to the query
function filter_where($where = '') {
    // posts from yesterday into the future
    $where .= ' AND post_date >= "' . date('Y-m-d', strtotime('-1 day')) . '"';
    return $where;
}

And in your code above you could do:

在上面的代码中,您可以执行以下操作:

$args = array('post_type' => 'event', 'posts_per_page' => 50, 'order' => 'ASC');
add_filter('posts_where', 'filter_where');
$loop = new WP_Query($args);
remove_filter('posts_where', 'filter_where');
if ( have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();

The adding and removing filter doesn't make this the most elegant solution, so you could probably clean it up by defining a custom function get_recent_and_future_posts() in your theme's functions.php that returns whatever object $loop is.

添加和删​​除过滤器不会使这成为最优雅的解决方案,因此您可以通过在主题的functions.php中定义自定义函数get_recent_and_future_posts()来清理它,该函数返回$ loop对象。

#2


1  

I took a look at: http://codex.wordpress.org/Class_Reference/WP_Query

我看了一下:http://codex.wordpress.org/Class_Reference/WP_Query

Down the page there's a section called "Time Parameters".

在页面下方有一个名为“时间参数”的部分。

I think instead of looking for post_status in the future you want to look for posts with a date greater than current date - 1 day.

我认为将来你不想寻找post_status而是想查找日期大于当前日期的帖子 - 1天。


推荐阅读
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 本文整理了315道Python基础题目及答案,帮助读者检验学习成果。文章介绍了学习Python的途径、Python与其他编程语言的对比、解释型和编译型编程语言的简述、Python解释器的种类和特点、位和字节的关系、以及至少5个PEP8规范。对于想要检验自己学习成果的读者,这些题目将是一个不错的选择。请注意,答案在视频中,本文不提供答案。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • Netty源代码分析服务器端启动ServerBootstrap初始化
    本文主要分析了Netty源代码中服务器端启动的过程,包括ServerBootstrap的初始化和相关参数的设置。通过分析NioEventLoopGroup、NioServerSocketChannel、ChannelOption.SO_BACKLOG等关键组件和选项的作用,深入理解Netty服务器端的启动过程。同时,还介绍了LoggingHandler的作用和使用方法,帮助读者更好地理解Netty源代码。 ... [详细]
  • node.jsurlsearchparamsAPI哎哎哎 ... [详细]
  • 本文介绍了协程的概念和意义,以及使用greenlet、yield、asyncio、async/await等技术实现协程编程的方法。同时还介绍了事件循环的作用和使用方法,以及如何使用await关键字和Task对象来实现异步编程。最后还提供了一些快速上手的示例代码。 ... [详细]
  • 使用Flutternewintegration_test进行示例集成测试?回答首先在dev下的p ... [详细]
  • 影响版本:WordPressCevherShare2.0plugin开发者:http:phpkode.com下载地址:http:phpkode.comdownloadscevher ... [详细]
  • 系列目录Guava1:概览Guava2:Basicutilities基本工具Guava3:集合CollectionsGuava4:GuavacacheGuava6:Concurre ... [详细]
  • Forexperiencedcryptoinvestors,thereareseveralsectorsthatseemedpromisingbutdidn’tlive ... [详细]
  • Wordpress实现点击文章标题跳转到外部链接的方法,有时候出于某些目的的需要,需要在wordpress博客中实现点击文章标题不直接跳转转到文章页面,而跳转到外部链接的效果。不过WordPr ... [详细]
author-avatar
sdfasdfqg
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有