php - 从 Woocommerce 中的 Order Notes 获取数组中的最后一项
问题描述
我想在发送给客户的电子邮件中包含最后的订单备注。目前它输出所有订单备注,我如何编辑此 foreach 以仅显示最后/最新订单备注?
安装使用 PHP 7.2,但理想情况下,我希望它适用于所有版本,我是否应该退回到我之前在测试站点上使用过的 PHP 5.6
<h2><?php _e( 'Order Notes', 'woocommerce' ); ?></h2>
<?php
$args = array(
'status' => 'approve',
'post_id' => $order->id
);
$comments = get_comments($args);
foreach($comments as $comment) :
echo $comment->comment_content . '<br />';
endforeach;
?>
因此,期望的结果将显示“订单状态从暂停更改为已完成”
解决方案
您可以摆脱 foreach 语句并使用如下end()
函数:
$lastcomment = end($comments);
echo $lastcomment->comment_content . '<br />';
推荐阅读
- ssrs-2012 - 即使没有数据也显示表格边框
- heroku - Heroku:“heroku ps:exec”和“heroku run bash”有什么区别?
- r - 在多个观测值的移动 48 小时窗口内增加大于 0.3
- python - 如何将文件的局部变量导入当前模块
- javascript - 如何在 for 循环中获取 mousedown 条件?
- java - 线程如何看到安全初始化对象的过时引用
- javascript - 网站在托管服务器上缺少部分,但在 localhost 上运行良好
- ruby-on-rails - 未定义的家务方法图:Stringacts_as_taggable_on
- c# - 如何将一个字符串 [] 值传递给一个查询并将字符串 [] 保留在一个 for 循环中
- highcharts - 导出一个包含阿拉伯文本和 highcharts div 的 div