如何将wordpress短代码更改为数组

添加数组时出现语法错误。 有人会指出我犯了哪个错误吗?</ p>

  function commresi(){
ob_start();
?&gt;

&lt;?php if(has_term = array('commercial','commercial-filtration','commercial-water-softeners','category')){?&gt;
&lt; p class =“commercial com-res “&gt;&lt; a href =”/ home“&gt;访问住宅系统&lt; / a&gt;&lt; / p&gt;
&lt;?php}其他{?&gt;
&lt; p class =”not-commercial com- res“&gt;&lt; a href =”/ commercial“&gt;访问Commercial Systems&lt; / a&gt;&lt; / p&gt;
&lt;?php}?&gt;

&lt;?php
return ob_get_clean() ;
}
add_shortcode('comres','commresi');
</ code> </ pre>
</ div>

展开原文

原文

I get a syntax error when I add an array. Will someone point out where I have made a mistake?

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');

3个回答



忘了在ob_start之前打开php标签,在声明has_term变量时忘记美元符号($)并忘记关闭php标签 在代码的末尾。</ p>

  function commresi()
{
&lt;?php
ob_start();
?&gt;

&lt;?php

if if($ has_term = array(
'commercial',
'commercial - filtering',
'commercial-water-softeners','category'
))

{?&gt;
&lt; p class =“commercial com-res”&gt;&lt; a href =“/ home”&gt;访问住宅系统&lt; / a&gt;&lt; / p&gt;
&lt;?php

}
其他
{?&gt;
&lt; p class =“not-commercial com-res”&gt;&lt; a href =“/ commercial”&gt;访问商业系统&lt; / a&gt;&lt; / p&gt;
&lt;?php
}?&gt;

&lt;?php
return ob_get_clean();
?&gt;
}

add_shortcode('comres','commresi'); \ n </ code> </ pre>
</ div>

展开原文

原文

forgot to open a php tag before ob_start, forgot the dollar sign($) when declaring the has_term variable and forgot to close the php tag at the end of the code.

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');

duanlou7910
duanlou7910 编辑添加编码风格atm ...
接近 3 年之前 回复



在has_term varibles之前忘记$(符号)</ strong> </ p>

 <  code> function commresi(){
ob_start();
?&gt;

&lt;?php if($ has_term = array('commercial','commercial-filtration','commercial-water-softeners','category')){?&gt;
&lt; p class =“commercial com -res“&gt;&lt; a href =”/ home“&gt;访问住宅系统&lt; / a&gt;&lt; / p&gt;
&lt;?php}其他{?&gt;
&lt; p class =”not- 商业广告“&gt;&lt; a href =”/ commercial“&gt;访问商业系统&lt; / a&gt;&lt; / p&gt;
&lt;?php}?&gt;

&lt;?php
return ob_get_clean();
}
add_shortcode('comres','commresi'

);
</ code> </ pre>
</ div>

展开原文

原文

Forgot the $(sign) before has_term varibles

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'

);



使用此代码:</ p>

  function commresi(){
$ commercial_array = array('commercial','commercial-filtration','commercial-water-softeners','category');
$ return ='&lt; p class =“not-commercial com-res”&gt;&lt; a href =“/ commercial”&gt;访问商业系统&lt; / a&gt;&lt; / p&gt;';

if(in_array($ has_term,$ commercial_array)){
$ return ='&lt; p class =“commercial com -res“&gt;&lt; a href =”/ home“&gt;访问住宅系统&lt; / a&gt;&lt; / p&gt;';
}
return $ return;

}

add_shortcode('comres','commresi');
</ code> </ pre>

其中$ has_term是您要与$ commercial_array匹配的术语。</ p >
</ div>

展开原文

原文

use this code :

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');

where $has_term is term you want to match from $commercial_array.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐