我有以下问题。我有一个wordpress循环查询两种类型的内容;每个都有一个foreach循环。第一个foreach循环查询google maps地图的lat/long值。由于输出限制,我只能在wp循环的范围内直接输出lat/long foreach循环。第二个循环我必须保存到一个变量中,然后在wp循环之外输出到一个单独的div中。
foreach($addrterms as $addrterm):
if( $termi->term_id == $addrterm ) {
$pinGlyphs = get_field( 'location_glyph' );
$pinColors = term_description($addrterm, 'thelocations');
$gmurl = get_field( 'location_url' );
if( !empty($gmurl) ) {
$addressTemplate .= '<article class="mapaddress" style="background-color:#'.$pinColors.'">
<h2><span>'.$pinGlyphs.'</span>'.get_the_title().'</h2>
<p>'.get_field( 'location_street' ).'</p>
<p><span>'.get_field( 'location_postalcode' ).'</span> <span>'.get_field( 'location_city' ).'</p>
<a href="http://'.echo $gmurl.'" target="_blank">'.echo $gmurl.'</a>
</article>';
}
else {
$addressTemplate .= '<article class="mapaddress" style="background-color:#'.$pinColors.'">
<h2><span>'.$pinGlyphs.'</span>'.get_the_title().'</h2>
<p>'.get_field( 'location_street' ).'</p>
<p><span>'.get_field( 'location_postalcode' ).'</span> <span>'.get_field( 'location_city' ).'</p>
</article>';
}
}
endforeach
关于代码片段。if语句检查当前地址是否被选择显示在google maps地图下面。在内部,特定位置的唯一字形存储在变量$pinGlyphs
中,唯一颜色存储在变量$pinColors
中。接下来,url被存储到变量$gmurl
中。但有一个约束,不是每个位置都必须有url。接下来我检查变量是否被填满了!empty。
在这种情况下,它是空的,没有url的标记被.=
附加到我的模板变量$addressTemplate。这没有问题。如果输入了特定位置的url,则第一个模板块与第二个块相比有额外的一行:
<a href="http://'.echo $gmurl.'" target="_blank">'.echo $gmurl.'</a>
在包含的wp循环之外,我在后面放置了以下代码片段:
<div class="addresses">
<?php if(!empty( $addressTemplate )){
echo $addressTemplate;
}?>
</div>
问题是页面根本不呈现。对于包含a
元素的行,我得到以下php错误:
[08-Jul-2014 17:11:43 UTC] PHP Parse error: syntax error, unexpected 'echo' (T_ECHO) in /Applications/MAMP/htdocs/mysite/wp-content/themes/mysite/front-page.php on line 476
如果我注释掉a
元素行,一切工作。有没有办法让它正确地输出' if (!empty($gmurl))大小写?还有一种方法可以使代码更短,节省几行,而不是编写整个标记或多或少两次,只有a元素的行作为差异?敬祝Ralf
Echo命令不能在字符串中使用。从你的代码中删除'echo'
<a href="http://'. $gmurl .'" target="_blank">'. $gmurl .'</a>
为了更好地阅读代码,在变量和点之间留一个空格。