我正在尝试构建一个循环,以根据页面ID显示不同的位置名称。我正在尝试for循环,但不知道如何获得2个结果,我尝试了foreach,但仍然迷路了。
$location = array( "Frisco" => "1507", "McKinney" => "1509");
$count = count($location);
for($i = 0; $i < $count; $i++){
if(is_page($location[$i])) {
$location = "";
}
}
Need results =
// Add Shortcode
function dynamicLocation(){
if(is_page('1')){
$location = "McKinney";
}
else if(is_page()){
$location....
}
}
答案 0 :(得分:2)
忘记for
,请使用foreach
:
foreach ($location as $key=>$value){
//here $key is the location and $value is the ID
//e.g. $key="Frisco", $value="1507"
}