- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我在 WordPress 上问过这个问题Stack Exchange 并且它在 24 小时后没有得到太多响应。所以我想我会把它带到更大的社区。
无论如何,我正在创建一个可以正常工作的事件插件,但是我在确定列表页面时遇到了一些麻烦。它显示所有事件,但我希望它按月对它们进行分组。该语句需要拉出事件的日期,获取月份,并将属于该特定月份的记录分组在一起。我知道这将是一个 foreach,但我不确定如何编写它。
这是我的循环:
// Query Post Type
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'meta_key' => '_eDate',
'orderby' => 'meta_value_num'
);
$the_query = new WP_Query($args);
// Build It
if ($the_query->have_posts()) :
?>
<div id="event-list">
<?php
global $post;
$month_array = array();
while ($the_query->have_posts()) : $the_query->the_post();
var_dump( get_post_meta( $post->ID, '_eDate', true ) );
$date_field = get_post_meta( $post->ID, '_eDate', true );
$month_format = new DateTime();
$month_format->createFromFormat('Y-m-d', $date_field);
$month_number = $month_format->format('m');
$month_array[] = $month_number;
if ($the_query != 0 && $month_number != $month_array[$the_query->current_post - 1]) //LINE 38
echo '<h2>' . $month_format->format( 'F' ) . '</h2>';
?>
<div class="row">
<div class="event-image">
<a href="<?php echo get_permalink(get_the_ID()); ?>">
<?php
if (has_post_thumbnail()) {
the_post_thumbnail('thumbnail');
}
?>
</a>
</div>
<div class="event-content">
<h3><a href="<?php echo get_permalink(get_the_ID()); ?>"><?php the_title(); ?></a></h3>
<div class="event-date"><?php display_event_date(); ?></div>
<div class="event-time"><?php display_event_time(); ?></div>
<div class="event-price"><?php display_event_price(); ?></div>
<br/>
<a href="<?php echo get_permalink(get_the_ID()); ?>" class="event-info">More Information</a>
</div>
<div class="event-buy">
<?php display_event_buy_online_url(); ?>
</div>
</div>
</div>
<?php wp_reset_postdata(); ?>
<?php
endwhile;
endif;
---编辑 1---
我已经根据输入更新了我的代码块。
var_dump 这样输出 string(10) "2015-07-09"
我也遇到了两个错误。
Notice: Object of class WP_Query could not be converted to int in C:\xampp\apps\wordpress\htdocs\wp-content\plugins\wp-events-em4b\views\shortcode.php on line 38
Notice: Undefined offset: -1 in C:\xampp\apps\wordpress\htdocs\wp-content\plugins\wp-events-em4b\views\shortcode.php on line 38
从 int 转换为文本的月份是错误的,它提取的是上次编辑帖子的时间,但 var_dump 具有正确的日期。
最佳答案
正如我在 WPSE 上对您的问题的回答中所述,您需要在自定义字段中按日期对帖子进行排序。这就像将正确的参数添加到查询参数一样简单
如果您的日期以以下格式保存,Y-m-d
,这很容易。您的年份将保持不变,因此您可以在技术上按月排序。
您只需将正确的值添加到 order
和 orderby
参数。您可以尝试以下方法
// Query Post Type
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'meta_key' => '_eDate',
'orderby' => 'meta_value'
);
如果您需要按月排序而不考虑年份,那么您需要在执行循环之前使用 usort()
对循环进行排序。如果是这种情况,请告诉我,以便我可以相应地调整我的答案。
您已经声明您的自定义字段中的日期格式是 Y-m-d
,因此您的循环应该根据您的自定义字段中的日期正确地按日期排序 _eDate
您现在需要做的就是检查循环中当前帖子与循环中上一篇帖子之间的日期(月份),如果它们不同,则输出月份。您不需要需要一个foreach
循环,您也不需要输出缓冲或使用原始帖子数组中的帖子创建一个新数组
以下代码未经测试,另外,请确保自定义字段中的日期格式正确。在您的循环中,您首先要执行 var_dump( get_post_meta( $post->ID, '_eDate', true ) );
以验证您是否以正确的格式获得了正确的值。我代码中的所有内容都依赖于这一小段信息。如果您的 _eDate
自定义字段中的值或格式不正确,我的代码将失败。请根据需要调整、修改和滥用我的代码
// Query Post Type
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'meta_key' => '_eDate',
'orderby' => 'meta_value' //HAD A BUG HERE, DID NOT SORT CORRECTLY, NEEDS TO BE meta_value
);
$the_query = new WP_Query($args);
// Build It
if ( $the_query->have_posts() ) :
?>
<div id="event-list">
<?php
$month_array = array();
while ( $the_query->have_posts() ) :
$the_query->the_post();
$date_field = get_post_meta( $post->ID, '_eDate', true );
$month_format = DateTime::createFromFormat( 'Y-m-d', $date_field ); // HAD A BUG HERE, CONVERTED WRONG DATE
$month_number = $month_format->format('m');
$month_array[] = $month_number;
// Choose the format you want to display as indicated below
if ( $the_query->current_post == 0 ) // Output date id this is the first post
echo '<h2>' . $month_format->format( 'F' ) . '</h2>'; // Output month name as January
if ( $the_query->current_post != 0 //HAD A BUG HERE, WAS $the_query != 0
&& $month_number != $month_array[$the_query->current_post - 1]
)
echo '<h2>' . $month_format->format( 'F' ) . '</h2>'; // Output month name as January
?>
<div class="row">
<div class="event-image">
<a href="<?php echo get_permalink(get_the_ID()); ?>">
<?php
if (has_post_thumbnail()) {
the_post_thumbnail('thumbnail');
}
?>
</a>
</div>
<div class="event-content">
<h3><a href="<?php echo get_permalink(get_the_ID()); ?>"><?php the_title(); ?></a></h3>
<div class="event-date"><?php display_event_date(); ?></div>
<div class="event-time"><?php display_event_time(); ?></div>
<div class="event-price"><?php display_event_price(); ?></div>
<br/>
<a href="<?php echo get_permalink(get_the_ID()); ?>" class="event-info">More Information</a>
</div>
<div class="event-buy">
<?php display_event_buy_online_url(); ?>
</div>
</div>
</div>
<?php wp_reset_postdata(); ?>
<?php
endwhile;
endif;
我已经修复了上面的代码并进行了测试。现在一切都按预期进行。请查看代码中的注释以了解已修复的错误
关于php - Foreach循环按类别分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31005975/
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 9 个月前关闭。 Improve
我使用 partykit打包并遇到以下错误消息: Error in matrix(0, nrow = mi, ncol = nl) : invalid 'nrow' value (too large
我一直在尝试寻找一个量表或分类指标,为 VADER 情绪分析分配一些情感程度,而不仅仅是积极、消极或中性。如果有人可以分享他们的观点或资源来帮助按以下方式对 VADER 复合分数进行分类,我将非常感激
伙计们,我想自动循环..但我不知道是我放错了 while 还是循环错了? 我的数据库 标签:kt_barang kd_kategori | nama_kategori 1
我正在创建一个列出本地企业并按类别、子类别和关键字对它们进行分组的应用程序。以下是企业排序规则: 一个企业可以属于多个类别和子类别 一个企业可以有多个关键字 并非每个类别都有子类别,但有子类别的只有两
我有一系列单词 - 我的刺激 - 它们显示在屏幕上。然而,每个词都有另一个“条件”,即它们是类别 A、类别 B 或类别 C。这可能很简单,但我找不到答案并坚持下去。我的最终目标是在每次运行脚本时将类别
我正在使用 Laravel 5.5 和 MySql。如果不向 Services 表中添加 subCategoryID 列,我无法弄清楚如何将类别和子类别与服务相关联。 目前这是我的表结构 服务类别 i
我有两个部分/类别结构的链接表。 the section table structure id sec_title 1 section 1 2 section 2 the category str
我有一个类层次结构如下 @interface PTLDatasource : NSObject ... @interface PTLFetchedDatasource : PTLDatasource
我有一个 DataFrame df 一列,category 使用以下代码创建: import pandas as pd import random as rand from string import
我经常在多个类中设置获取请求以从核心数据(加上一些其他结果)中检索“allRecipes”或“lastModifiedDate”。 为此使用专门的类别 NSManagedObjectContext+R
可以在 Objective C 中创建类别之间的依赖关系吗?也在类别和它们的基类之间? 我知道在运行时应该没有区别,它们可能只是在编译时合并在一起。例如,假设我将 B 类分解为: B(base cla
这个问题在这里已经有了答案: 关闭 10 年前。
example img of a category selection by user 嘿,我正在尝试设置一个选择,用户必须选择一个类别和第二个类别,但我不知道如何获取他单击的信息。用户单击类别后,它
尝试将投资组合库添加到我正在制作的自定义 wp 主题中。我已经筋疲力尽地试图寻找甚至可以修改一些的解决方案和插件。我认为我在寻找解决方案时遇到的一个问题是我不完全确定哪些搜索词可以帮助我找到与我想要实
当我查看 Cocoa Touch API 时,我可以在同一个头文件中找到一些与类别一起声明的类,例如 @interface NSArray : NSObject @property (readonl
我的 log4j.properties 中有以下内容 log4j.rootLogger = debug, stdout, fileLog log4j.appender.stdout = org.apa
如果我在类中添加类别方法,比如NSXMLNode: @interface NSXMLNode (mycat) - (void)myFunc; @end NSXMLNode 的子类,例如 NSXMLEl
先说场景,wordpress的分类结构是这样的 Level 1: Top Level 2: -Nextme_1 Level 3: --Nextme_2 --Nextme_3 Leve
我有一个解析网络,现在我想浏览标签,或显示图表。我怎样才能得到图表?或者在树中导航。显示第一步然后其他等。并了解这棵树是如何 build 的。 import urllib from lxml impo
我是一名优秀的程序员,十分优秀!