【发布时间】:2018-08-13 19:51:48
【问题描述】:
我一直试图在手册中阅读此内容,但基本上我有一个array,我试图将其反转并排除最后一项。所以我的数组中目前有 14 个项目,我正在让它反转,它显示 14-2。我的代码让它排除了最后一项。所以我猜它在技术上是可行的,但我实际上希望它输出为 13-1。我尝试使用array_pop 和array_shift,但我不知道如何将它与array_reverse 集成。
function smile_gallery( $atts ) {
if( have_rows('smile_gallery', 3045) ):
$i = 1;
$html_out = '';
$html_out .= '<div class="smile-container">';
$html_out .= '<div class="fg-row row">';
// vars
$rows = get_field('smile_gallery', 3045);
$count = count( get_field('smile_gallery', 3045) );
$html_out .= '<div class="col-md-8">'; // col-md-offset-1
$html_out .= '<div class="smile-thumbs">';
foreach( array_reverse($rows) as $row) :
// vars
$week = "smile_week";
$img = "smile_img";
$caption = "smile_caption";
// Do stuff with each post here
if( $i < $count) :
$html_out .= '<div class="smile-thumb-container">';
$html_out .= '><h6>Week ' . $row["smile_week"] . '</h6></a>'; // smile thumb week
$html_out .= '</div>'; // smile thumb container
endif;
$i++;
endforeach;
$html_out .= '</div>';
$html_out .= '</div>';
$html_out .= '</div>';
$html_out .= '</div>'; // smile container
endif;
return $html_out;
}
add_shortcode( 'show_smiles', 'smile_gallery' );
【问题讨论】:
-
将 array_reverse 移出 foreach 并在之前进行修改。
-
@LawrenceCherone 如果我把它拿出来,
foreach不会反转
标签: php foreach count advanced-custom-fields