简体   繁体   中英

How can I change my wordpress shortcode to an array

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

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

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'

);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM