我在 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/
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我脑子里浮现出一些关于一种新编程语言的想法,所以我想我会尝试实现它。一位friend建议我尝试使用Treetop(Rubygem)来创建一个解析器。Treetop的文档很少,我以前从未做过这种事情。我的解析器表现得好像有一个无限循环,但没有堆栈跟踪;事实证明很难追踪到。有人可以指出入门级解析/AST指南的方向吗?我真的需要一些列出规则、常见用法等的东西来使用像Treetop这样的工具。我的语法分析器在GitHub上,以防有人希望帮助我改进它。class{initialize=lambda(name){receiver.name=name}greet=lambda{IO.puts("He
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
假设我有一个在Ruby中看起来像这样的哈希:{:ie0=>"Hi",:ex0=>"Hey",:eg0=>"Howdy",:ie1=>"Hello",:ex1=>"Greetings",:eg1=>"Goodday"}有什么好的方法可以将它变成如下内容:{"0"=>{"ie"=>"Hi","ex"=>"Hey","eg"=>"Howdy"},"1"=>{"ie"=>"Hello","ex"=>"Greetings","eg"=>"Goodday"}} 最佳答案 您要求一个好的方法来做到这一点,所以答案是:一种您或同事可以在六个月后理解
我是Ruby的新手,有些闭包逻辑让我感到困惑。考虑这段代码:array=[]foriin(1..5)array[5,5,5,5,5]这对我来说很有意义,因为i被绑定(bind)在循环之外,所以每次循环都会捕获相同的变量。使用每个block可以解决这个问题对我来说也很有意义:array=[](1..5).each{|i|array[1,2,3,4,5]...因为现在每次通过时都单独声明i。但现在我迷路了:为什么我不能通过引入一个中间变量来修复它?array=[]foriin1..5j=iarray[5,5,5,5,5]因为j每次循环都是新的,我认为每次循环都会捕获不同的变量。例如,这绝对
我已经有很多两个值数组,例如下面的例子ary=[[1,2],[2,3],[1,3],[4,5],[5,6],[4,7],[7,8],[4,8]]我想把它们分组到[1,2,3],[4,5],[5,6],[4,7,8]因为意思是1和2有关系,2和3有关系,1和3有关系,所以1,2,3都有关系我如何通过ruby库或任何算法来做到这一点? 最佳答案 这是基本Bron–Kerboschalgorithm的Ruby实现:classGraphdefinitialize(edges)@edges=edgesenddeffind_maximum_
如果至少有两个相邻的数字相同,格式为,我需要打包.这是我的输入:[2,2,2,3,4,3,3,2,4,4,5]以及预期的输出:"2:3,3,4,3:2,2,4:2,5"到目前为止我试过:a=[1,1,1,2,2,3,2,3,4,4,5]a.each_cons(2).any?do|s,t|ifs==t如果相等,也许可以尝试计数器,但那是行不通的。 最佳答案 您可以使用Enumerable#chunk_while(如果你使用的是Ruby>=2.3):a.chunk_while{|a,b|a==b}.flat_map{|chunk|chu
假设我有一个没有特定顺序的随机数数组。假设这些是参加马拉松比赛的人的ID#,他们按照完成的顺序添加到数组中,例如:race1=[8,102,67,58,91,16,27]race2=[51,31,7,15,99,58,22]这是一个简化且有些做作的示例,但我认为它传达了基本思想。现在有几个问题:首先,我如何获得特定条目之前和之后的ID?假设我正在查看运行者58,我想知道谁在他之前和之后完成了比赛。race1,runner58:previousfinisher=67,nextfinisher=91race2,runner58:previousfinisher=99,nextfinishe
defreverse(ary)result=[]forresult[0,0]inaryendresultendassert_equal["baz","bar","foo"],reverse(["foo","bar","baz"])这行得通,我想了解原因。有什么解释吗? 最佳答案 如果我使用each而不是for/in重写它,它看起来像这样:defreverse(ary)result=[]#forresult[0,0]inaryary.eachdo|item|result[0,0]=itemendresultendforainb基本上就