这段代码选择了一个包含3个索引的数组:
<?php var_dump($the_query1->posts); ?>
输出是3个索引的数组:
array(3) {
[0]=>
object(WP_Post)#4451 (24) {
["ID"]=>
int(19163)
["post_author"]=>
string(1) "2"
["post_date"]=>
string(19) "2018-09-28 15:42:12"
["post_date_gmt"]=>
string(19) "2018-09-28 13:42:12"
["post_content"]=>
string(0) ""
["post_title"]=>
string(16) "Global Factories"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(6) "closed"
["ping_status"]=>
string(6) "closed"
["post_password"]=>
string(0) ""
["post_name"]=>
string(16) "global-factories"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2018-11-15 12:41:57"
["post_modified_gmt"]=>
string(19) "2018-11-15 11:41:57"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(48) "https://www.zeo.nl/?post_type=cases&p=19163"
["menu_order"]=>
int(0)
["post_type"]=>
string(5) "cases"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
}
[1]=>
object(WP_Post)#4448 (24) {
["ID"]=>
int(19167)
["post_author"]=>
string(1) "2"
["post_date"]=>
string(19) "2018-09-21 14:58:50"
["post_date_gmt"]=>
string(19) "2018-09-21 12:58:50"
["post_content"]=>
string(0) ""
["post_title"]=>
string(24) "Warmteservice Groep B.V."
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(6) "closed"
["ping_status"]=>
string(6) "closed"
["post_password"]=>
string(0) ""
["post_name"]=>
string(13) "warmteservice"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2018-11-15 12:00:56"
["post_modified_gmt"]=>
string(19) "2018-11-15 11:00:56"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(48) "https://www.zeo.nl/?post_type=cases&p=19167"
["menu_order"]=>
int(0)
["post_type"]=>
string(5) "cases"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
}
[2]=>
object(WP_Post)#4556 (24) {
["ID"]=>
int(19157)
["post_author"]=>
string(1) "2"
["post_date"]=>
string(19) "2018-09-21 11:26:12"
["post_date_gmt"]=>
string(19) "2018-09-21 09:26:12"
["post_content"]=>
string(0) ""
["post_title"]=>
string(9) "Blauwtulp"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(6) "closed"
["ping_status"]=>
string(6) "closed"
["post_password"]=>
string(0) ""
["post_name"]=>
string(11) "blauwtulp-2"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2018-11-15 12:46:48"
["post_modified_gmt"]=>
string(19) "2018-11-15 11:46:48"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(48) "https://www.zeo.nl/?post_type=cases&p=19157"
["menu_order"]=>
int(0)
["post_type"]=>
string(5) "cases"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
}
}
检查当前索引是否在位置1的最佳方法是什么?我想返回一个布尔值来确定该元素应该变成<div class="col-sm-4">
还是<div class="col-sm-3">
,以防返回true。
我尝试过:
<?php var_dump($the_query1->posts[1] === true); ?>
但这将返回三遍false
。我需要返回false
,true
,false
。最简单的检查方法是什么?
答案 0 :(得分:1)
使用foreach循环数据,并在数组键为1时执行匹配条件。
foreach ($the_query1->posts as $key => $value) {
if($key == 1){
echo '<div class="col-sm-3">';
} else {
echo '<div class="col-sm-4">';
}
echo '</div>';
}