[英]How can I change my wordpress shortcode to an array
添加数组时出现语法错误。 有人会指出我做错了什么吗?
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');
忘记在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');
在has_term变量之前忘记了$(sign)
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'
);
使用此代码:
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匹配的术语。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.