gpt4 book ai didi

php - Foreach循环按类别分组

转载 作者:可可西里 更新时间:2023-10-31 23:29:57 25 4
gpt4 key购买 nike

我在 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,这很容易。您的年份将保持不变,因此您可以在技术上按月排序。

您只需将正确的值添加到 orderorderby 参数。您可以尝试以下方法

// 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com