热门标签 | 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 编程中的常见问题,并提供了丰富的代码示例和解决方案,帮助开发者更好地理解和应用 PHP 知识。 ... [详细]
  • Python 异步编程:深入理解 asyncio 库(上)
    本文介绍了 Python 3.4 版本引入的标准库 asyncio,该库为异步 IO 提供了强大的支持。我们将探讨为什么需要 asyncio,以及它如何简化并发编程的复杂性,并详细介绍其核心概念和使用方法。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍了如何使用PHP检测AJAX请求,通过分析预定义服务器变量来判断请求是否来自XMLHttpRequest。此方法简单实用,适用于各种Web开发场景。 ... [详细]
  • PHP 5.2.5 安装与配置指南
    本文详细介绍了 PHP 5.2.5 的安装和配置步骤,帮助开发者解决常见的环境配置问题,特别是上传图片时遇到的错误。通过本教程,您可以顺利搭建并优化 PHP 运行环境。 ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 机器学习中的相似度度量与模型优化
    本文探讨了机器学习中常见的相似度度量方法,包括余弦相似度、欧氏距离和马氏距离,并详细介绍了如何通过选择合适的模型复杂度和正则化来提高模型的泛化能力。此外,文章还涵盖了模型评估的各种方法和指标,以及不同分类器的工作原理和应用场景。 ... [详细]
  • Active Object设计模式解析
    Active Object设计模式通过引入代理(Proxy)和命令(Command)模式,实现了方法调用与执行的解耦,从而支持异步操作和提高系统的并发处理能力。本文将深入探讨Active Object模式的工作原理及其应用场景。 ... [详细]
  • 本文探讨了在支付项目开发中使用SS5 Socket Server实现内部网络访问外部网络的技术方案。详细介绍了SS5的安装、配置及性能测试过程,旨在为面临相同需求的技术人员提供参考。 ... [详细]
  • 开发笔记:图像分割基于阙值+边缘检测+区域法图像分割matlab源码含GUI
    开发笔记:图像分割基于阙值+边缘检测+区域法图像分割matlab源码含GUI ... [详细]
  • 本文探讨了如何利用 Application 对象在 PHP 应用程序中共享数据,特别是在多用户环境中保持数据的一致性和安全性。文章还介绍了 Application 对象的基本结构、方法和事件,并提供了实际应用示例。 ... [详细]
  • 本文提供了详细的指导,帮助开发者了解如何使用PHP插件进行网站内容的翻译,特别是针对WordPress插件和主题的汉化及多语言支持。 ... [详细]
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社区 版权所有