当我添加一个数组时,我得到一个语法错误。有人能指出我哪里出了错吗?
function commresi() {
ob_start();
?>
<?php if( has_term=array('commercial',’commercial-filtration’,'commercial-water-softeners’,’category') ) { ?>
<p class="commercial com-res"><a href="/home">Visit Residential Systems</a></p>
<?php } else { ?>
<p class="not-commercial com-res"><a href="/commercial">Visit Commercial Systems</a></p>
<?php } ?>
<?php
return ob_get_clean();
}
add_shortcode('comres', 'commresi');发布于 2017-10-25 11:39:05
在ob_start之前忘记打开php标签,在声明has_term变量时忘记美元符号($),在代码末尾忘记关闭php标签。
function commresi()
{
<?php
ob_start();
?>
<?php
if ($has_term = array(
'commercial',
’commercial - filtration’,
'commercial-water-softeners’,’category'
))
{ ?>
<p class="commercial com-res"><a href="/home">Visit Residential Systems</a></p>
<?php
}
else
{ ?>
<p class="not-commercial com-res"><a href="/commercial">Visit Commercial Systems</a></p>
<?php
} ?>
<?php
return ob_get_clean();
?>
}
add_shortcode('comres', 'commresi');发布于 2017-10-25 13:08:05
在has_term变量之前,忘记了$(符号)
function commresi() {
ob_start();
?>
<?php if( $has_term=array('commercial',’commercial-filtration’,'commercial-water-softeners’,’category') ) { ?>
<p class="commercial com-res"><a href="/home">Visit Residential Systems</a></p>
<?php } else { ?>
<p class="not-commercial com-res"><a href="/commercial">Visit Commercial Systems</a></p>
<?php } ?>
<?php
return ob_get_clean();
}
add_shortcode('comres', 'commresi'
);发布于 2017-10-25 13:39:17
使用以下代码:
function commresi() {
$commercial_array = array('commercial','commercial-filtration','commercial-water-softeners','category');
$return = ' <p class="not-commercial com-res"><a href="/commercial">Visit Commercial Systems</a></p>';
if( in_array ($has_term, $commercial_array) ) {
$return = ' <p class="commercial com-res"><a href="/home">Visit Residential Systems</a></p>';
}
return $return;
}
add_shortcode('comres', 'commresi');其中,$has_term是您希望从$commercial_array中匹配的术语。
https://stackoverflow.com/questions/46923513
复制相似问题